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>"; } ?> Quote Link to comment 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) Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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" Quote Link to comment 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>"; } ?> Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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 :'( :'( Quote Link to comment 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) Quote Link to comment 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); } Quote Link to comment 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 Quote Link to comment 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!!! Quote Link to comment 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.