Insert Issue

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Mactek
Forum Commoner
Posts: 28
Joined: Tue Oct 28, 2003 1:19 pm
Location: Boston

Insert Issue

Post by Mactek »

Go easy on my I'm a rookie at PHP.

When i do this insert, my ID will insert to my MySQL db but the rest of my fields are blank. Here are my two pages of code:

Page One:
insert.htm

Code: Select all

<html>
 <head>
 </head>
 <center>
 <form method="post" action="script.php">
 <input type="hidden" name="id" value="null">
 <table>
 <tr><td align="left">First</td>
 <td><input type="text" name="first"></td>
 </tr>
 <tr><td align="left">Last</td>
 <td><input type="text" name="last" size="20"></td>
 </tr>
 <tr><td align="left">Phone</td>
 <td><input type="text" name="phone" size="20"></td>
 </tr>
 <tr><td align="left">Mobile</td>
 <td><input type="text" name="mobile" size="20"></td>
 </tr>
 <tr><td align="left">Fax</td>
 <td><input type="text" name="fax" size="20"></td>
 </tr>
 <tr><td align="left">E-Mail</td>
 <td><input type="text" name="email" size="20"></td>
 </tr>
 <tr><td align="left">Web</td>
 <td><input type="text" name="web" size="20"></td>
 </tr>
 <tr><td colspan="2">
 <p align="center">
 <input type="submit" value="Enter record">
 </td>
 </tr>
 </table>
 </form>
 </center>
 </html>
Page Two:
script.php

Code: Select all

<?

 $username="username";
 $password="password";
 $database="tutorial";
 
 mysql_connect(localhost,$username,$password);
 @mysql_select_db($database) or die( "Unable to select database");

 $query = "INSERT INTO contacts VALUES('$id','$first','$last','$phone','$mobile','$fax','$email','$web')";
 
 mysql_query($query);

 mysql_close();
 
 print "<html><body><center>";
 print "<p>You have just entered this record<p>";
 print "First Name : $first<br>";
 print "Last Name : $last<br>";
 print "Phone : $Phone<br>";
 print "Mobile : $mobile<br>";
 print "Fax : $fax<br>";
 print "E-mail : $email<br>";
 print "Web : $web<br>";
 print "</body></html>";

?>
Anyone have any ideas? Much appreciation, thanks!!
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

Post by AVATAr »

try changing

$query = "INSERT INTO contacts VALUES('$id','$first','$last','$phone','$mobile','$fax','$email','$web')";

and use $_POST variable to catch the form variables like $_POST['first'] , $_POST['last'] and so on
Mactek
Forum Commoner
Posts: 28
Joined: Tue Oct 28, 2003 1:19 pm
Location: Boston

Post by Mactek »

I tried that and now i'm getting a parse error on the query.
Draco_03
Forum Regular
Posts: 577
Joined: Fri Aug 15, 2003 12:25 pm
Location: Montreal, Canada

Post by Draco_03 »

use brackets around it.. like

Code: Select all

{$_POST[$last]}
Mactek
Forum Commoner
Posts: 28
Joined: Tue Oct 28, 2003 1:19 pm
Location: Boston

Post by Mactek »

That does not work either. I know that PHP is send the query because it is making a new row in my DB but only with the ID. None of my vars are getting passed into MySQL. Is there a setting within MySQL that I need to change?
Draco_03
Forum Regular
Posts: 577
Joined: Fri Aug 15, 2003 12:25 pm
Location: Montreal, Canada

Post by Draco_03 »

Code: Select all

"INSERT INTO Contacts ( last ) VALUES ( {$_POST[$last]})";
aomething like that should work...
Mactek
Forum Commoner
Posts: 28
Joined: Tue Oct 28, 2003 1:19 pm
Location: Boston

Post by Mactek »

No Dice. I must have something setup wrong in MySQL.
Post Reply