Jump to content

Recommended Posts

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();

Link to comment
https://forums.phpfreaks.com/topic/266107-mysqli-insert-not-working-properly/
Share on other sites

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!';
}

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!';
}

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

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;
}
?>

 

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.

@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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.