Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP Databases
| Search | Today's Posts | Mark Forums Read
PHP Databases Using PHP in conjunction with databases. PHP questions not specific to databases should be directed to one of the other PHP forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old October 11th, 2005, 01:52 PM
Registered User
 
Join Date: Apr 2005
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Cannot INSERT INTO mySQL database anymore!

I am foiled ladies and gentlemen! I have used the code below in two other previous projects where it has worked just fine for the past three years (for those particular projects). However, I'm building an admin area for a friend and he probably has the latest version of mySQL and/or PHP. Consequently, the code I have been using does not seem to work for me now. I'm not sure if a change in mySQL and/or PHP is the source of the problem, but I'm thinking it could be???

Here's the scenario: I am collecting data from a form (insert.html). When the form is submitted, the form action calls addrecord.php, which is in the same directory as insert.html. I put the addrecord.php file in the <form name="Form1" action="addrecord.php" method="post"> as shown here. Although I'm completely filling out all of the fields on the form, the only items that seem to be collected from insert.html and written to the database when the form is submitted are:

$partnerid = ([integer] [primary key] [auto_increments by 1])
$signup = (date function [m/d/y])
$adminpasscode = (a hidden field being passed from insert.html that I defined with a variable on addrecord.php)

Everything else is missing and I'm not sure why!

My guess is that I have to "catch" all of the values from insert.html and "store" them in variables in addrecord.php, which will have to be inserted into the field names in the database. I really don't have any clue how to do the coding, if this is the solution. Please help. I'm using PHP & MySQL for DUMMIES for pete's sake! The book speaks about using the $_POST function, but does not give any "working" example of how this works hand-in-hand with the INSERT INTO statement. Everything seems to focus mainly on the SELECT FROM statement. It is a fairly good book for beginners, though.

Below, is the PHP and mySQL code I'm working with. Any suggestions? Please show "working" related example using appropriate variables as they relate to the field names I have listed. I need to know what exactly goes into the pages, "addrecord.php" and "insert.html". Please, please, please show the actual code in place. This has been a real problem in the past where someone gives an example (using variables that do not apply in a way that I don't understand) and then makes fun of me for not understanding the "lack of" detailed solution that was provided. Once I've seen it done right, I'll know how to do it forever. I'm not understanding exactly where and how to properly use $_POST (i.e. $_POST[my_db_field];) In either case, I appreciate any of your help that you can give. After all, isn't that what these boards are for? Thanks a lot for your help, and here's the code...


Code:
<?
// A file I'm using to store database connection information.
include("connectmeinc.php"); 

$connection = mysql_connect($host, $user, $password) or die ("Couldn't connect to server.");

$db = mysql_select_db($database, $connection) or die ("Couldn't select database.");

// This (signup) is a variable that I assigned to insert the date, which indicates the date each record is entered into the database.
$signup = date("m/d/y");

// I want to insert the password, "admin" into every record. It is a hidden field that is passed from the form, "insert.html".
$adminpasscode = "admin";

$query = "INSERT INTO toolChest (partnerid,firstname,lastname,category,bizname,address,suite,city,state,zipcode,phonearea,phoneprefix,phonesuffix,faxarea,faxprefix,faxsuffix,email,webaddress,product1,product2,product3,description,signup,adminpasscode) VALUES ('$partnerid','$firstname','$lastname','$category','$bizname','$address','$suite','$city','$state','$zipcode','$phonearea','$phoneprefix','$phonesuffix','$faxarea','$faxprefix','$faxsuffix','$email','$webaddress','$product1','$product2','$product3','$description','$signup','$adminpasscode')";
$result = mysql_query($query) or die ("Could Not Insert Into Database.");

?>
Thanks!
William Watson

  #2 (permalink)  
Old October 24th, 2005, 04:19 AM
Friend of Wrox
 
Join Date: Mar 2004
Location: Bhopal, MP, India.
Posts: 357
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to anshul
Default

Can you tell us the errors, that's showing?
Use ini_set('display_errors', '1'); if PHP is not showing the errors.

Are you sure $signup = date("m/d/y"); is correct.
Just get it out, to ensure: die($signup);

`~@#\^%&*/\.<.\/-|+|_!:;..=?>
Support Indian students' finances http://scholarship.mediasworks.com/
  #3 (permalink)  
Old December 10th, 2005, 01:07 AM
Registered User
 
Join Date: Apr 2005
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I finally figured out how to use $_POST. Here's what I did and it worked great!

<?php
include("connectme.php");

$connection = mysql_connect($host,$user,$password)
or die ("Couldn't Connect To Server.");

$db = mysql_select_db($database,$connection)
or die ("Couldn't Select Database.");

$category = $_POST['category'];
$bizname = addslashes($_POST['bizname']);
$address = addslashes($_POST['address']);
$address2 = addslashes($_POST['address2']);
$city = addslashes($_POST['city']);
$state = $_POST['state'];
$zipcode = $_POST['zipcode'];
$email = addslashes($_POST['email']);
$webaddress = addslashes($_POST['webaddress']);
$product1 = $_POST['product1'];
$product2 = $_POST['product2'];
$product3 = $_POST['product3'];
$description = addslashes($_POST['description']);
$signup = date("m/d/y");
$passcode = $_POST['passcode'];
$phonearea = $_POST['phonearea'];
$phoneprefix = $_POST['phoneprefix'];
$phonesuffix = $_POST['phonesuffix'];

$query = "INSERT INTO partners
(category,bizname,address,address2,city,state,zipc ode,
email,webaddress,product1,product2,product3,descri ption,
signup,passcode,phonearea,phoneprefix,phonesuffix)
VALUES
('$category','$bizname','$address','$address2','$c ity',
'$state','$zipcode','$email','$webaddress','$produ ct1',
'$product2','$product3','$description','$signup',' $passcode',
'$phonearea','$phoneprefix','$phonesuffix')";

$result = mysql_query($query) or die ("Could Not Insert Record Into Database.");

header("Location: http://www.some-web-site.com/admin/index.php");
?>



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to insert images to MySQL (BLOB) wkm1925 ASP.NET 2.0 Professional 0 November 19th, 2006 06:29 AM
INSERT with MySQL sethtrain BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 4 August 1st, 2006 06:38 AM
empty fields not hidden anymore (php/mySQL) jasmin Beginning PHP 1 January 17th, 2005 12:42 PM
cannot insert into mysql:data LogicalDevloper Pro PHP 1 December 22nd, 2004 09:08 AM
insert null value in MySql? donh BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 2 November 28th, 2003 11:55 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.