ciaran1987 Posted April 16, 2009 Share Posted April 16, 2009 Hi Guys I was wondering if you could help me with a problem I am having.I`m trying to create a PHP file that enters information into 3 separate fields into a table row when the user enters information into a HTML form. at the moment I have created this <html> <head> <title>update database</title> </head> <body> <form method="post" action="update.php"> Please state the category of problem that has occurred<br/> <input type="text" name="category" size="70"/></br> Please give a desription of the problem</br> <input type="text" name="problem" size="70"/></br> Please give a solution to the problem<br/> <input type="text" name="solution" size="70"/></br> <input type="submit" value="Submit the problem and solution"/> </form> </body> </html> <?php $category = $_POST['category']; mysql_connect ("localhost", "root","password") or die (mysql_error()); mysql_select_db ("heskdb"); $query="insert into hesk_std_replies (category) values ('$category')"; mysql_query($query) or die ('error updating database'); echo"database updated with:".$category; $solution = $_POST['solution']; mysql_connect ("localhost", "root","password") or die (mysql_error()); mysql_select_db ("heskdb"); $query="insert into hesk_std_replies (solution) values ('$solution')"; mysql_query($query) or die ('error updating database'); echo"database updated with:".$solution; $problem = $_POST['problem']; mysql_connect ("localhost", "root","password") or die (mysql_error()); mysql_select_db ("heskdb"); $query="insert into hesk_std_replies (problem) values ('$problem')"; mysql_query($query) or die ('error updating database'); echo"database updated with:".$problem; ?> The problem with this is thatit enters the inormation into 3 incremental rows where I want it to be entered into the same row. I have created this file which I thaught would work but it is giving me errors. gdds <?php $category = $_POST['category']; $problem = $_POST['problem']; $solution = $_POST['solution']; mysql_connect ("localhost", "root","password") or die (mysql_error()); mysql_select_db ("heskdb"); $query="insert into hesk_std_replies(category,problem,solution) values ('.$category.''.$problem.''.$solution.')"; mysql_query($query) or die ('error updating database'); echo"database updated with:".$category ,$problem,$solution; ?> Any help would be much appreciated Quote Link to comment https://forums.phpfreaks.com/topic/154394-solved-inserting-information-into-an-sql-db/ Share on other sites More sharing options...
Mchl Posted April 16, 2009 Share Posted April 16, 2009 Your second solution is almost good. You've left concatenation operators in your query (also know as 'dots'): $query="insert into hesk_std_replies(category,problem,solution) values ('.$category.''.$problem.''.$solution.')"; it should be $query="insert into hesk_std_replies(category,problem,solution) values ('$category','$problem','$solution')"; Quote Link to comment https://forums.phpfreaks.com/topic/154394-solved-inserting-information-into-an-sql-db/#findComment-811787 Share on other sites More sharing options...
Psycho Posted April 16, 2009 Share Posted April 16, 2009 The last echo statement will fail as well <?php $category = mysql_real_escape_string($_POST['category']); $problem = mysql_real_escape_string($_POST['problem']); $solution = mysql_real_escape_string($_POST['solution']); mysql_connect ("localhost", "root","password") or die (mysql_error()); mysql_select_db ("heskdb"); $query="INSERT INTO `hesk_std_replies` (`category`, `problem`, `solution`) values ('{$category}', '{$problem}', '{$solution}')"; mysql_query($query) or die ('error updating database'); echo "Database updated with: {$category}, {$problem}, {$solution}"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/154394-solved-inserting-information-into-an-sql-db/#findComment-811791 Share on other sites More sharing options...
Mchl Posted April 16, 2009 Share Posted April 16, 2009 It would look ugly, but there's no reason why it would fail. Quote Link to comment https://forums.phpfreaks.com/topic/154394-solved-inserting-information-into-an-sql-db/#findComment-811800 Share on other sites More sharing options...
ciaran1987 Posted April 16, 2009 Author Share Posted April 16, 2009 Your second solution is almost good. You've left concatenation operators in your query (also know as 'dots'): $query="insert into hesk_std_replies(category,problem,solution) values ('.$category.''.$problem.''.$solution.')"; it should be $query="insert into hesk_std_replies(category,problem,solution) values ('$category','$problem','$solution')"; Thanks for the reply but I am still having the same error IE error updating database Quote Link to comment https://forums.phpfreaks.com/topic/154394-solved-inserting-information-into-an-sql-db/#findComment-811801 Share on other sites More sharing options...
Mchl Posted April 16, 2009 Share Posted April 16, 2009 And what does it say? Quote Link to comment https://forums.phpfreaks.com/topic/154394-solved-inserting-information-into-an-sql-db/#findComment-811803 Share on other sites More sharing options...
ciaran1987 Posted April 16, 2009 Author Share Posted April 16, 2009 And what does it say? Its the "die" message -error updating database mysql_query($query) or die ('error updating database'); Quote Link to comment https://forums.phpfreaks.com/topic/154394-solved-inserting-information-into-an-sql-db/#findComment-811805 Share on other sites More sharing options...
Mchl Posted April 16, 2009 Share Posted April 16, 2009 Use die(mysql_error()." $query") to display something meaningful Quote Link to comment https://forums.phpfreaks.com/topic/154394-solved-inserting-information-into-an-sql-db/#findComment-811809 Share on other sites More sharing options...
Psycho Posted April 16, 2009 Share Posted April 16, 2009 It would look ugly, but there's no reason why it would fail. You're right. I've never seen any documentation that the comma works concatenate text the same as a period. Although it only seems to work within an echo and not for an assignment. Quote Link to comment https://forums.phpfreaks.com/topic/154394-solved-inserting-information-into-an-sql-db/#findComment-811833 Share on other sites More sharing options...
Mchl Posted April 16, 2009 Share Posted April 16, 2009 Yeah. That's special for echo. Quote Link to comment https://forums.phpfreaks.com/topic/154394-solved-inserting-information-into-an-sql-db/#findComment-811837 Share on other sites More sharing options...
ciaran1987 Posted April 16, 2009 Author Share Posted April 16, 2009 Use die(mysql_error()." $query") to display something meaningful Thanks ,the error is Column count doesn't match value count at row 1 insert into hesk_std_replies(category,problem,solution) values ('hardware''Broken Mouse''Connection') Quote Link to comment https://forums.phpfreaks.com/topic/154394-solved-inserting-information-into-an-sql-db/#findComment-811876 Share on other sites More sharing options...
Mchl Posted April 16, 2009 Share Posted April 16, 2009 To my knowledge it sounds like you don't have commas between the values you're inserting. Quote Link to comment https://forums.phpfreaks.com/topic/154394-solved-inserting-information-into-an-sql-db/#findComment-811881 Share on other sites More sharing options...
ciaran1987 Posted April 16, 2009 Author Share Posted April 16, 2009 To my knowledge it sounds like you don't have commas between the values you're inserting. Exellent stuff cheers Quote Link to comment https://forums.phpfreaks.com/topic/154394-solved-inserting-information-into-an-sql-db/#findComment-811892 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.