Jump to content

Saving to a SQL Database using php and wamp


twinytwo

Recommended Posts

Hi guys

 

Im looking to save the contents of 4 textboxes to a mySql db. Using wamp, phpmyadmin etc.

 

?php
$Name = $_Post('Name');
$Age = $_Post('Age');
$Sex = $_Post('Sex');
$Address_1 = $_Post('Address_1');

mysql_connect("127.0.0.1","user","") or die ('Error: ' .mysql_error());
mysql_select_db("user");

$query ="INSERT INTO users(Name,Age,Sex,Address_1) VALUES('".$Name."', '".$Age."','".$Sex."','".$Address_1."');

mysql_query($query) or die ('Error updating datadase');
mysql_Close();

?>

This is the code im using, it dosent seem to do anything to the database..... can someone point out where im going wrong... thanks

Post using the code tags

 

<?php
$Name = $_POST['Name'];
$Age = $_POST['Age'];
$Sex = $_POST['Sex'];
$Address_1 = $_POST['Address_1'];

mysql_connect("127.0.0.1","user","password") or die ('Error: ' .mysql_error());
mysql_select_db("user");
$query ="INSERT INTO users(Name,Age,Sex,Address_1) VALUES('$Name', $Age','$Sex','$Address_1')";
mysql_query($query) or die ('Error updating datadase');
mysql_close();
?>

 

Missed the POST and also the square brackets, must be a little tired

Should the POST values be uppercased?

Did you look in your error log?  Your code has several obvious syntax errors.

 

The syntax for accessing arrays is to use [] not ().  Also $_POST is the name of the superglobal array... all caps.

 

$Name = $_POST['Name'];

 

Also, php supports interpolation when you use double quotes.  The values of variables will be inserted into the string, so your code will be alot easier to read if you utilize it rather than concatenation.

 

$query ="INSERT INTO users (Name,  Age, Sex, Address_1) VALUES ('$Name', '$Age', '$Sex', '$Address_1')";

 

What database type is the Age column?  If it's not a string you would not include the single quotes around it in the values section of the query.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.