cutielou22 Posted July 23, 2012 Share Posted July 23, 2012 Not sure if this is the right place for this or not but . . . I am having trouble with a mysqli code - I am not sure what the problem is and it doesn't give me any errors. It simply does not insert the information to the database. Here it is: $addband = $mysqli->prepare("INSERT INTO bands (id, bandname, bandurl, bio, status, twitter, facebook, myspace, youtube, itunes, website, hometown, genre, genre1, genre2, genre3, genre4, recordlabel, email, manager, bookingagent, extra, tags) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)") or die(mysqli_error()); $addband->bind_param('sssbssssssssssssssssss', $blank, $bandname, $bandurl, $bandbio, $status, $twitter, $facebook, $myspace, $youtube, $itunes, $website, $hometown, $genre, $genre1, $genre2, $genre3, $genre4, $recordlabel, $email, $manager2, $bookagent2, $extra, $tags); $addband->execute(); $addband->fetch(); $addband->close(); Quote Link to comment https://forums.phpfreaks.com/topic/266107-mysqli-insert-not-working-properly/ Share on other sites More sharing options...
peipst9lker Posted July 23, 2012 Share Posted July 23, 2012 Is your $mysqli-resource properly connected to the database? If so, please post the output of var_dump($addband, $mysqli); (place it under your prepare statement) Also It's useful to check for the return value prepare() gives out e.g. if ($select = $mysqli->prepare("SELECT foo FROM bar")) { // bindparam,execute, etc. } else { echo 'prepare() failed!'; } Quote Link to comment https://forums.phpfreaks.com/topic/266107-mysqli-insert-not-working-properly/#findComment-1363648 Share on other sites More sharing options...
cutielou22 Posted July 23, 2012 Author Share Posted July 23, 2012 It still didn't add data to the database and no errors came up. Here is what I tried: if ($addband = $mysqli->prepare("INSERT INTO band (id, bandname, bandurl, bio, status, twitter, facebook, myspace, youtube, itunes, website, hometown, genre, genre1, genre2, genre3, genre4, recordlabel, email, manager, bookingagent, extra, tags) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")) { var_dump($addband, $mysqli); $addband->bind_param('sssbssssssssssssssssss', $blank, $bandname, $bandurl, $bandbio, $status, $twitter, $facebook, $myspace, $youtube, $itunes, $website, $hometown, $genre, $genre1, $genre2, $genre3, $genre4, $recordlabel, $email, $manager2, $bookagent2, $extra, $tags); $addband->execute(); $addband->fetch(); $addband->close(); header("Location: addband.php?note=$bandname+has+been+added."); } else { echo 'prepare() failed!'; } Quote Link to comment https://forums.phpfreaks.com/topic/266107-mysqli-insert-not-working-properly/#findComment-1363828 Share on other sites More sharing options...
jazzman1 Posted July 23, 2012 Share Posted July 23, 2012 Start from very beginning: <?php $mysqli = new mysqli("localhost", "user", "pass", "database"); /* check connection */ if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } // continue with prepare statement Quote Link to comment https://forums.phpfreaks.com/topic/266107-mysqli-insert-not-working-properly/#findComment-1363830 Share on other sites More sharing options...
cutielou22 Posted July 23, 2012 Author Share Posted July 23, 2012 Nope. Nothing. :/ Quote Link to comment https://forums.phpfreaks.com/topic/266107-mysqli-insert-not-working-properly/#findComment-1363834 Share on other sites More sharing options...
jazzman1 Posted July 23, 2012 Share Posted July 23, 2012 Check out this guide -> http://php.net/manual/en/mysqli.quickstart.prepared-statements.php Quote Link to comment https://forums.phpfreaks.com/topic/266107-mysqli-insert-not-working-properly/#findComment-1363842 Share on other sites More sharing options...
jazzman1 Posted July 23, 2012 Share Posted July 23, 2012 Take a note: Also, consider the use of the MySQL multi-INSERT SQL syntax for INSERTs. For the example, multi-INSERT requires less round-trips between the server and client than the prepared statement shown above <?php if (!$mysqli->query("INSERT INTO test(id) VALUES (1), (2), (3), (4)")) { echo "Multi-INSERT failed: (" . $mysqli->errno . ") " . $mysqli->error; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/266107-mysqli-insert-not-working-properly/#findComment-1363843 Share on other sites More sharing options...
jcbones Posted July 23, 2012 Share Posted July 23, 2012 bind_param will fail, as you only passed 22 types (1st param), but 23 variables to it. You are also sending your fourth variable as a blob, which chunks it out. Pretty sure you have to send that with mysqli-stmt.send_long_data(), but I could be wrong on that. Quote Link to comment https://forums.phpfreaks.com/topic/266107-mysqli-insert-not-working-properly/#findComment-1363845 Share on other sites More sharing options...
cutielou22 Posted July 23, 2012 Author Share Posted July 23, 2012 @jcbones You were right I checked that over and over again thinking that may have been a problem and still didn't catch it. @jazzman1 Now it does give an error using: if (!$mysqli->query("INSERT INTO band (id, bandname, bandurl, bio, status, twitter, facebook, myspace, youtube, itunes, website, hometown, genre, genre1, genre2, genre3, genre4, recordlabel, email, manager, bookingagent, extra, tags) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")) { echo "Multi-INSERT failed: (" . $mysqli->errno . ") " . $mysqli->error; } else{ if ($addband = $mysqli->prepare("INSERT INTO band (id, bandname, bandurl, bio, status, twitter, facebook, myspace, youtube, itunes, website, hometown, genre, genre1, genre2, genre3, genre4, recordlabel, email, manager, bookingagent, extra, tags) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")) { var_dump($addband, $mysqli); $addband->bind_param('sssssssssssssssssssssss', $blank, $bandname, $bandurl, $bandbio, $status, $twitter, $facebook, $myspace, $youtube, $itunes, $website, $hometown, $genre, $genre1, $genre2, $genre3, $genre4, $recordlabel, $email, $manager2, $bookagent2, $extra, $tags); $addband->execute(); $addband->fetch(); $addband->close(); header("Location: addband.php?note=$bandname+has+been+added."); } else { echo 'prepare() failed!'; } } The error given: Multi-INSERT failed: (1064) You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/266107-mysqli-insert-not-working-properly/#findComment-1363850 Share on other sites More sharing options...
cutielou22 Posted July 23, 2012 Author Share Posted July 23, 2012 I got it working! Not sure exactly how though . . . thanks for the help you guys! Quote Link to comment https://forums.phpfreaks.com/topic/266107-mysqli-insert-not-working-properly/#findComment-1363871 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.