简体   繁体   中英

bind_param issue PHP prepared Insert statement

I have a user reg form which then inserts into the db however its throwing up a bind param error

if(isset($_POST['submit'])){
// Login Query
// Connection
$mysqli = new mysqli("localhost", "root", "", "pg");
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
// Query 
$query = "INSERT INTO `affiliates` (aid,affname,title,fname,lname,add1,add2,add3,city,county,country,pcode,email,password,paymethod) VALUES (,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
$stmt = $mysqli->prepare($query);
$stmt->bind_param('ssssssssssssss',$_POST['affname'],$_POST['title'],$_POST['fname'],$_POST['lname'],$_POST['add1'],$_POST['add2'],$_POST['add3'],$_POST['city'],$_POST['county'],$_POST['country'],$_POST['pcode'],$_POST['email'],$_POST['password'],$_POST['paymethod']); // If 2x Variables are used etc 's' would become 'ss',$_GET['VAR'],$_GET['VAR']
// Bind Results
$stmt->execute();
//$result = $stmt->get_result()->fetch_all();
}

I cant work out whats wrong can anyone help thanks


Updated Form & Query Code

        <form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
    <p>
    <label for="textfield"></label>
    <input type="text" name="affname" id="textfield" />
    </p>
    <p>
    <label for="textfield"></label>
    <input type="text" name="title" id="textfield" />
    </p> <p>
    <label for="textfield"></label>
    <input type="text" name="fname" id="textfield" />
    </p> <p>
    <label for="textfield"></label>
    <input type="text" name="lname" id="textfield" />
    </p> <p>
    <label for="textfield"></label>
    <input type="text" name="add1" id="textfield" />
    </p> <p>
    <label for="textfield"></label>
    <input type="text" name="add2" id="textfield" />
    </p> <p>
    <label for="textfield"></label>
    <input type="text" name="add3" id="textfield" />
    </p> <p>
    <label for="textfield"></label>
    <input type="text" name="city" id="textfield" />
    </p> <p>
    <label for="textfield"></label>
    <input type="text" name="county" id="textfield" />
    </p> <p>
    <label for="textfield"></label>
    <input type="text" name="country" id="textfield" />
    </p> <p>
    <label for="textfield"></label>
    <input type="text" name="pcode" id="textfield" />
    </p> <p>
    <label for="textfield"></label>
    <input type="text" name="email" id="textfield" />
    </p>
    <p>
    <label for="textfield"></label>
    <input type="text" name="password" id="textfield" />
    </p>
    <p>
    <label for="textfield"></label>
    <input type="text" name="paymethod" id="textfield" />
    <input type="hidden" value="" name="aid" />
    </p>
    <p>
    <input type="submit" name="submit" id="submit" value="Submit" />
    </p>
    </form>
    <?php
    if(isset($_POST['submit'])){
    // Login Query
    // Connection
    $mysqli = new mysqli("localhost", "root", "", "pg");
    if ($mysqli->connect_errno) {
      echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
    }
    // Query 
    $query = "INSERT INTO `affiliates` (aid,affname,title,fname,lname,add1,add2,add3,city,county,country,pcode,email,password,paymethod) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    $stmt = $mysqli->prepare($query);
    $stmt->bind_param('sssssssssssssss',$_POST['aid'],$_POST['affname'],$_POST['title'],$_POST['fname'],$_POST['lname'],$_POST['add1'],$_POST['add2'],$_POST['add3'],$_POST['city'],$_POST['county'],$_POST['country'],$_POST['pcode'],$_POST['email'],$_POST['password'],$_POST['paymethod']); // If 2x Variables are used etc 's' would become 'ss',$_GET['VAR'],$_GET['VAR']
    // Bind Results
    $stmt->execute();
    //$result = $stmt->get_result()->fetch_all();
    }

I have amended the query and now have the correct amount of vars being passed However it now shows NO errors but also does not insert the data

Change

$query = "INSERT INTO `affiliates` (aid,affname,title,fname,lname,add1,add2,add3,city,county,country,pcode,email,password,paymethod) VALUES (,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

to :

$query = "INSERT INTO `affiliates` (aid,affname,title,fname,lname,add1,add2,add3,city,county,country,pcode,email,password,paymethod) VALUES (NULL,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

or to

$query = "INSERT INTO `affiliates` (affname,title,fname,lname,add1,add2,add3,city,county,country,pcode,email,password,paymethod) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

由于您的帮助是自动递增的,因此只需将查询更改为此,就无需将其插入查询中。

$query = "INSERT INTO affiliates (affname,title,fname,lname,add1,add2,add3,city,county,country,pcode,email,password,paymethod) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

you trying to insert 15 columns

1)aid

2)affname

3)title

4)fname

5)lname

6)add1

7)add2

8)add3

9)city

10)county

11)country

12)pcode

13)email

14)password

15)paymethod

but here you are metioning only 14
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 
,?,?,?,?,?,?,?,?,?,?, ?, ?, ?, ?
 s s s s s s s s s s  s  s  s  s

as well as here only 14

$_POST['affname'],$_POST['title'],$_POST['fname'],$_POST['lname'],$_POST['add1'],$_POST['add2'],$_POST['add3'],$_POST['city'],$_POST['county'],$_POST['country'],$_POST['pcode'],$_POST['email'],$_POST['password'],$_POST['paymethod']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM