joshgarrod Posted December 3, 2008 Share Posted December 3, 2008 Hi guys, noob here as you will guess, I am having trouble with this script, it merely refreshes the page when you click the submit button, what am I doing wrong? <?php (POST METHOD) if ($REQUEST_METHOD=="POST") { $SQL = " INSERT INTO awnings "; $SQL = $SQL . " (Make, Model, Size, Year, Price, Description) VALUES "; $SQL = $SQL . " ('$Make','$Model','$Size','$Year, $Price, $Description') "; $result = mysql_db_query($db,"$SQL",$cid); $ID=mysql_insert_id(); if (!$result) { echo("ERROR: " . mysql_error() . "\n$SQL\n"); } echo ("<P><B>New awning has been added, thank you</B></P>\n"); } ?> <FORM NAME="fa" ACTION="add_used_awnings.php" METHOD="POST"> Make: <INPUT NAME="Make" TYPE="text" id="Make" SIZE=40><br /><br /> Model: <INPUT TYPE="Model" NAME="Model" VALUE="" SIZE=15><br /><br /> Size: <input name="Size" type="text" id="Size" value="" size="15" /><br /><br /> Year: <input name="Year" type="text" id="Year" value="" size="15" /><br /><br /> Price: <input name="Price" type="text" id="Price" value="£" size="15" /><br /><br /> Description: <textarea name="Description" cols="50" rows="10" id="Description" type="text" value="" /></textarea><br /><br /> <input name="submit" type="submit" value="Add stock" /> </FORM> Link to comment https://forums.phpfreaks.com/topic/135438-solved-problems-inserting-data-into-database/ Share on other sites More sharing options...
gevans Posted December 3, 2008 Share Posted December 3, 2008 <?php if ($_POST['submit']) { $SQL = " INSERT INTO awnings "; $SQL .= " (Make, Model, Size, Year, Price, Description) VALUES "; $SQL .= " ('$Make','$Model','$Size','$Year, $Price, $Description') "; $result = mysql_db_query($db,$SQL,$cid); $ID=mysql_insert_id(); if (!$result) { echo("ERROR: " . mysql_error() . "\n$SQL\n"); } echo ("<P><B>New awning has been added, thank you</B></P>\n"); } ?> <FORM NAME="fa" ACTION="add_used_awnings.php" METHOD="POST"> Make: <INPUT NAME="Make" TYPE="text" id="Make" SIZE=40><br /><br /> Model: <INPUT TYPE="Model" NAME="Model" VALUE="" SIZE=15><br /><br /> Size: <input name="Size" type="text" id="Size" value="" size="15" /><br /><br /> Year: <input name="Year" type="text" id="Year" value="" size="15" /><br /><br /> Price: <input name="Price" type="text" id="Price" value="£" size="15" /><br /><br /> Description: <textarea name="Description" cols="50" rows="10" id="Description" type="text" value="" /></textarea><br /><br /> <input name="submit" type="submit" value="Add stock" /> </FORM> Link to comment https://forums.phpfreaks.com/topic/135438-solved-problems-inserting-data-into-database/#findComment-705540 Share on other sites More sharing options...
joshgarrod Posted December 3, 2008 Author Share Posted December 3, 2008 it submits and says that the awning has been added but it doesn't actually make it to the database, do i need to include the ID? Link to comment https://forums.phpfreaks.com/topic/135438-solved-problems-inserting-data-into-database/#findComment-705546 Share on other sites More sharing options...
gevans Posted December 3, 2008 Share Posted December 3, 2008 Is that all your code? If so you haven't connected to your database Link to comment https://forums.phpfreaks.com/topic/135438-solved-problems-inserting-data-into-database/#findComment-705556 Share on other sites More sharing options...
joshgarrod Posted December 3, 2008 Author Share Posted December 3, 2008 no i have left out the database connection code for this post so its not that i dont think Link to comment https://forums.phpfreaks.com/topic/135438-solved-problems-inserting-data-into-database/#findComment-705559 Share on other sites More sharing options...
gevans Posted December 4, 2008 Share Posted December 4, 2008 OK, well post all your code, and we'll go from there Link to comment https://forums.phpfreaks.com/topic/135438-solved-problems-inserting-data-into-database/#findComment-705562 Share on other sites More sharing options...
joshgarrod Posted December 4, 2008 Author Share Posted December 4, 2008 <?php $usr = "user"; $pwd = "pword"; $db = "database"; $host = "host"; # connect to database $cid = mysql_connect($host,$usr,$pwd); if (!$cid) { echo("ERROR: " . mysql_error() . "\n"); } if ($_POST['submit']) { $SQL = " INSERT INTO awnings "; $SQL .= " (Make, Model, Size, Year, Price, Description) VALUES "; $SQL .= " ('$Make','$Model','$Size','$Year, $Price, $Description') "; $result = mysql_db_query($db,$SQL,$cid); $ID=mysql_insert_id(); if (!$result) { echo("ERROR: " . mysql_error() . "\n$SQL\n"); } echo ("<P><B>New awning has been added, thank you</B></P>\n"); } ?> <FORM NAME="fa" ACTION="add_used_awnings.php" METHOD="POST"> Make: <INPUT NAME="Make" TYPE="text" id="Make" SIZE=40><br /><br /> Model: <INPUT TYPE="Model" NAME="Model" VALUE="" SIZE=15><br /><br /> Size: <input name="Size" type="text" id="Size" value="" size="15" /><br /><br /> Year: <input name="Year" type="text" id="Year" value="" size="15" /><br /><br /> Price: <input name="Price" type="text" id="Price" value="£" size="15" /><br /><br /> Description: <textarea name="Description" cols="50" rows="10" id="Description" type="text" value="" /></textarea><br /><br /> <input name="submit" type="submit" value="Add stock" /> </FORM> <?php mysql_close($cid); ?> Link to comment https://forums.phpfreaks.com/topic/135438-solved-problems-inserting-data-into-database/#findComment-705566 Share on other sites More sharing options...
joshgarrod Posted December 4, 2008 Author Share Posted December 4, 2008 i am also getting this: ERROR: Column count doesn't match value count at row 1 INSERT INTO awnings (Make, Model, Size, Year, Price, Description) VALUES ('','','',', , ') Link to comment https://forums.phpfreaks.com/topic/135438-solved-problems-inserting-data-into-database/#findComment-705568 Share on other sites More sharing options...
gevans Posted December 4, 2008 Share Posted December 4, 2008 Firstly; $SQL .= " ('$Make','$Model','$Size','$Year, $Price, $Description') "; change that line to; $SQL .= " ('$Make','$Model','$Size','$Year', '$Price', '$Description') "; Secondly, you haven't assigned your variables; $Make = mysql_real_escape_string($_POST['Make']); $Model = mysql_real_escape_string($_POST['Model'); etc.... Link to comment https://forums.phpfreaks.com/topic/135438-solved-problems-inserting-data-into-database/#findComment-705571 Share on other sites More sharing options...
joshgarrod Posted December 4, 2008 Author Share Posted December 4, 2008 thanks very much works fine now thanks again Link to comment https://forums.phpfreaks.com/topic/135438-solved-problems-inserting-data-into-database/#findComment-705576 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.