richard_PHP Posted May 30, 2009 Share Posted May 30, 2009 the worlds most easiest bit of code and it aint working!!!! ive checked the database, the page thats sending the information and een this one to make sure everything is the same but it keeps displaying the error message. code: <?php $conn = mysql_connect("xxx", "xxx", "xxx"); mysql_select_db("xxx", $conn); $img = $_POST[img]; $thumb = $_POST[thumb]; $url = $_POST[url]; $title = $_POST[title]; $description = $_POST[description]; $type = $_POST[type]; $sql = "INSERT INTO xxx VALUES ('', '$_POST[img]', '$_POST[thumb]' '$_POST[url]', '$_POST[title]', '$_POST[description]', '$_POST[type]')"; $result = mysql_query($sql); if ($result) { echo "<p><strong>$title</strong> was added into the database. Would you like to:</p>"; echo "<p>- <a href='insert.html'>Add another</a></p>"; } else { echo "<p>There was a problem.</p>"; } ?> Link to comment https://forums.phpfreaks.com/topic/160259-solved-insert-into/ Share on other sites More sharing options...
Garethp Posted May 30, 2009 Share Posted May 30, 2009 Well, A) Find out what the error message is. Google mysql_error. B) shouldn't it be INSERT INTO `xxx` (column names VALUES (value names) Link to comment https://forums.phpfreaks.com/topic/160259-solved-insert-into/#findComment-845721 Share on other sites More sharing options...
richard_PHP Posted May 30, 2009 Author Share Posted May 30, 2009 had it like that before and it didnt work, changed it back and it still doesnt work. and the error message that comes up is the else statement error. Link to comment https://forums.phpfreaks.com/topic/160259-solved-insert-into/#findComment-845722 Share on other sites More sharing options...
Garethp Posted May 30, 2009 Share Posted May 30, 2009 echo mysql_error($result) then paste your error Link to comment https://forums.phpfreaks.com/topic/160259-solved-insert-into/#findComment-845724 Share on other sites More sharing options...
richard_PHP Posted May 30, 2009 Author Share Posted May 30, 2009 "Column count doesn't match value count at row 1" Link to comment https://forums.phpfreaks.com/topic/160259-solved-insert-into/#findComment-845730 Share on other sites More sharing options...
richard_PHP Posted May 30, 2009 Author Share Posted May 30, 2009 had a look into it and found that i needed '' instead of NULL for the id. doing this, it now says that the project was added to the database. when checking..... it isnt. heres the code as it stands at the moment: <?php // Start the connection to the database $conn = mysql_connect("xxx", "xxx", "xxx"); // Select the database to use mysql_select_db("xxx", $conn); // Assign the posted values as variables to use later $img = $_POST[img]; $thumb = $_POST[thumb]; $url = $_POST[url]; $title = $_POST[title]; $description = $_POST[description]; $type = $_POST[type]; // Create the MySQL command which adds the user's information into the database. $sql = "INSERT INTO xxx (`id`, `img`, `thumb`, `url`, `title`, `description`, `type`) VALUES ('', '$_POST[img]', '$_POST[thumb]' '$_POST[url]', '$_POST[title]', '$_POST[description]', '$_POST[type]')"; $result = mysql_query($sql); if (!$result) { echo "<p><strong>$title</strong> was added into the database. Would you like to:</p>"; echo "<p>- <a href='insert.html'>Add another</a></p>"; } else { echo "<p>There was a problem.</p>"; } ?> Link to comment https://forums.phpfreaks.com/topic/160259-solved-insert-into/#findComment-845733 Share on other sites More sharing options...
Garethp Posted May 30, 2009 Share Posted May 30, 2009 Ok, if it isn't inserting into the database, try this A) Delete your query B) Restart your query, add one column and test C) Add another column and test D) Add another column and test If you do that, until you've built your query, you'll either get it right or find the problem Link to comment https://forums.phpfreaks.com/topic/160259-solved-insert-into/#findComment-845735 Share on other sites More sharing options...
Michdd Posted May 30, 2009 Share Posted May 30, 2009 Edit: You have if(!$result), it should be if($result) which is why it's saying it's correct when it's not. Link to comment https://forums.phpfreaks.com/topic/160259-solved-insert-into/#findComment-845736 Share on other sites More sharing options...
Michdd Posted May 30, 2009 Share Posted May 30, 2009 Use this: $sql = 'INSERT INTO xxx (`id`, `img`, `thumb`, `url`, `title`, `description`, `type`) VALUES ("", ' . $_POST['img'] . ', ' . $_POST['thumb'] . ', ' . $_POST['url'] . ', ' . $_POST['title'] . ', ' . $_POST['description'] . ', ' . $_POST['type'] . ')'; Also, just to let you know, you don't have to include the ID in the inserting query if it's an auto-incrementing value. Link to comment https://forums.phpfreaks.com/topic/160259-solved-insert-into/#findComment-845740 Share on other sites More sharing options...
kickstart Posted May 30, 2009 Share Posted May 30, 2009 Hi The assignment of the variable from the $_POST array probably won't work as you are not putting quotes inside the square brackets, which I presume is why you are trying to use the $_POST array within the SQL. Try this:- <?php // Start the connection to the database $conn = mysql_connect("xxx", "xxx", "xxx"); // Select the database to use mysql_select_db("xxx", $conn); // Assign the posted values as variables to use later $img = $_POST['img']; $thumb = $_POST['thumb']; $url = $_POST['url']; $title = $_POST['title']; $description = $_POST['description']; $type = $_POST['type']; // Create the MySQL command which adds the user's information into the database. $sql = "INSERT INTO xxx (`id`, `img`, `thumb`, `url`, `title`, `description`, `type`) VALUES (NULL, '$img', '$thumb' '$url', '$title', '$description', '$type')"; $result = mysql_query($sql); if ($result) { echo "<p><strong>$title</strong> was added into the database. Would you like to:</p>"; echo "<p>- <a href='insert.html'>Add another</a></p>"; } else { echo "<p>There was a problem. <br /> $sql <br /> ".mysql_error()."</p>"; } ?> Note that you really should use mysql_real_escape_string for the variable to try and prevent SQL injection issues. All the best Keith Link to comment https://forums.phpfreaks.com/topic/160259-solved-insert-into/#findComment-845741 Share on other sites More sharing options...
richard_PHP Posted May 30, 2009 Author Share Posted May 30, 2009 tried all suggested methods and its still not working :'( :'( Link to comment https://forums.phpfreaks.com/topic/160259-solved-insert-into/#findComment-845744 Share on other sites More sharing options...
Michdd Posted May 30, 2009 Share Posted May 30, 2009 Just do this.. Bit messy since you gotta list all variables, but meh (Assuming id is an autoincrement) $img = $_POST['img']; $thumb = $_POST['thumb']; $url = $_POST['url']; $title = $_POST['title']; $description = $_POST['description']; $type = $_POST['type']; // Create the MySQL command which adds the user's information into the database. $sql = "INSERT INTO xxx (`img`, `thumb`, `url`, `title`, `description`, `type`) VALUES ('" . $img . "', '" . $thumb . "', '" . $url . "', '" . $title . "', '" . $description . "', '" . $type . "')"; (also there was a comma missing) Link to comment https://forums.phpfreaks.com/topic/160259-solved-insert-into/#findComment-845745 Share on other sites More sharing options...
Garethp Posted May 30, 2009 Share Posted May 30, 2009 Let's try to make that a little messy (For fun) foreach($_POST as $k => $v) { $$k = mysql_escape_string($v); } Link to comment https://forums.phpfreaks.com/topic/160259-solved-insert-into/#findComment-845747 Share on other sites More sharing options...
kickstart Posted May 30, 2009 Share Posted May 30, 2009 tried all suggested methods and its still not working :'( :'( When you have tried the method I have put and it has failed, what error message did it put out? All the best Keith Link to comment https://forums.phpfreaks.com/topic/160259-solved-insert-into/#findComment-845749 Share on other sites More sharing options...
richard_PHP Posted May 30, 2009 Author Share Posted May 30, 2009 i love you michdd!!!! lol.. it works at last! lol thank you all for the help, sorry if it seemed a pain. just thank god its working now! cheers!!! Link to comment https://forums.phpfreaks.com/topic/160259-solved-insert-into/#findComment-845751 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.