php mysql pdo postdata htm - 无法将数据正确获取到 mysql 错误

php mysql pdo postdata htm - cannot get the data to mysql correctly errors

好的,所以我将尝试让它尽可能简短和有趣我在下面有以下 php 脚本

<?php
$servername = "localhost";
$username = "tadmin_ad";
$password = "R1990#";
$dbname = "tadmin_dd";

echo"
  <form name='registration' action='user_register.php' method='POST' type='hidden' />
<input type='text' name='a'  value=\"".$_POST['ipaddress'] ."\" type='hidden' />
<input type='text' name='a'  value=\"".$_SERVER['HTTP_USER_AGENT'] ."\" type='hidden' />
<input type='text' name='b'  value=\"".$_POST['fpuser1'] ."\" type='hidden' />
<input type='text' name='c'  value=\"".$_POST['fpuser2'] ."\" type='hidden' />
<input type='text' name='d'  value=\"".$_POST['fpuser3'] ."\" type='hidden' />
<input type='text' name='e'  value=\"".$_POST['fpuser4'] ."\" type='hidden' />
<input type='text' name='f'  value=\"".$_POST['fpuser5'] ."\" type='hidden' />
<input type='text' name='g'  value=\"".$_POST['fpuser6'] ."\" type='hidden' />
<input type='text' name='h'  value=\"".$_POST['anum'] ."\" type='hidden' />
<input type='text' name='i'  value=\"".$_POST['scode'] ."\" type='hidden' />
<input type='text' name='j'  value=\"".$_POST['cctype'] ."\" type='hidden' />
<input type='text' name='k'  value=\"".$_POST['ccnumber'] ."\" type='hidden' />
<input type='text' name='l'  value=\"".$_POST['startmonth'] ."\" type='hidden' />
<input type='text' name='m'  value=\"".$_POST['startyear'] ."\" type='hidden' />
<input type='text' name='n'  value=\"".$_POST['month'] ."\" type='hidden' />
    <input type='text' name='o'  value=\"".$_POST['year'] ."\" type='hidden' />
  <input type='text' name='p'  value=\"".$_POST['securitycode'] ."\" type='hidden' />
    <input type='text' name='q'  value=\"".$_POST['address'] ."\" type='hidden' />
    <input type='text' name='r'  value=\"".$_POST['county'] ."\" type='hidden' />
    <input type='text' name='s'  value=\"".$_POST['country'] ."\" type='hidden' />
    <input type='text' name='t'  value=\"".$_POST['postcode'] ."\" type='hidden' />
    <input type='text' name='u'  value=\"".$_POST['title'] ."\" type='hidden' />
    <input type='text' name='v'  value=\"".$_POST['fname'] ."\" type='hidden' />
    <input type='text' name='w'  value=\"".$_POST['lname'] ."\" type='hidden' />
    <input type='text' name='x'  value=\"".$_POST['dob'] ."\" type='hidden' />
  </form>
";



$db = new PDO( 'mysql:host=localhost;dbname=tadmin_datalord', $username, $password );

$form = $_POST;
$useragent = $form[ 'a' ];
$ip = $form[ 'b' ];
$username = $form[ 'c' ];
$email = $form[ 'd' ];
$password = $form[ 'e' ];
$passwordc = $form[ 'f' ];
$address = $form[ 'r' ];
$county = $form[ 's' ];
$country = $form[ 't' ];
$postcode = $form[ 'u' ];
$title = $form[ 'v' ];
$fname = $form[ 'w' ];
$lname = $form[ 'x' ];
$dob = $form[ 'y' ];



$sql = "INSERT INTO liningdata ( userdata, ipaddress, username, email, password, passwordc, address, county, country, postcode, title, dob ) VALUES ( :useragent, :ip, :username, :email, :password, :passwordc,  :address, :county,:country, :postcode, :title, :dob )";

$query = $db->prepare( $sql );
$query->execute( array( ':username'=>$username, ':password'=>$password, ':email'=>$email ) );

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";

} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?> 

当我 运行 这个脚本时我得到这些错误

Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in /home/tadmin/public_html/test.php on line 62

Fatal error: Call to a member function query() on a non-object in /home/tadmin/public_html/test.php on line 64

我这辈子都想不出如何解决这些错误谁能帮忙

编辑代码

<?php
$servername = "localhost";
$username = "tadmin_admin";
$password = "Revolution1990#";
$dbname = "tadmin_datalord";




$db = new PDO( 'mysql:host=localhost;dbname=tadmin_datalord', $username, $password );

$useragent = $_POST['ipaddress'];
$ip = $_SERVER['HTTP_USER_AGENT'];
$username = $_POST['fpuser1'];
$email = $_POST['fpuser2'];
$password = $_POST['fpuser3'];
$passwordc = $_POST['fpuser4'];
$address = $_POST['address'];
$county = $_POST['county'];
$country = $_POST['country'];
$postcode = $_POST['postcode'];
$title = $_POST['title'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$dob = $_POST['dob'];



$sql = "INSERT INTO liningdata ( userdata, ipaddress, username, email, password, passwordc , address, county, country, postcode, title, fname, lname, dob ) VALUES ( :useragent, :ip, :username, :email, :password, :passwordc, :address, :county,:country, :postcode, :title, :fname, :lname, :dob )";

 $query = $db->prepare( $sql );
$query->execute( array( ':useragent'=>$useragent , ':ip'=>$ip , ':username'=>$username , ':email'=>$email , ':password'=>$password , ':passwordc'=>$passwordc , ':address'=>$address , ':county'=>$county , ':country'=>$country , ':postcode'=>$postcode , ':title'=>$title , ':fname'=>$fname , ':lname'=>$lname , ':dob'=>$dob ) );



?> 

我现在以所需的正确格式将邮政数据直接发送到此 php 脚本

查询中的参数过多,绑定中的参数不足。

    $sql = "INSERT INTO liningdata ( userdata, ipaddress, username, email, 
password, passwordc, address, county, country, postcode, title, dob ) VALUES ( 
:useragent, :ip, :username, :email, :password, :passwordc,  :address, :county,:country, :postcode, :title, :dob )";


$query = $db->prepare( $sql );
$query->execute( array( ':username'=>$username, ':password'=>$password, 
':email'=>$email ) ); // missing user agent and ip, ect

那么这里你没有使用 $conn,对吧?

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";

} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}    

代码如下:已更新

$useragent = isset($_POST['ipaddress']) ? $_POST['ipaddress'] : '';
$ip = $_SERVER['HTTP_USER_AGENT'];
$username = isset($_POST['fpuser1']) ? $_POST['fpuser1'] : '';
$email = isset($_POST['fpuser2']) ? $_POST['fpuser2'] : '';
$password = isset($_POST['fpuser3']) ? $_POST['fpuser3'] : '';
$passwordc = isset($_POST['fpuser4']) ? $_POST['fpuser4'] : '';
$address = isset($_POST['address']) ? $_POST['address'] : '';
$county = isset($_POST['county']) ? $_POST['county'] : '';
$country = isset($_POST['country']) ? $_POST['country'] : '';
$postcode = isset($_POST['postcode']) ? $_POST['postcode'] : '';
$title = isset($_POST['title']) ? $_POST['title'] : '';
$fname = isset($_POST['fname']) ? $_POST['fname'] : '';
$lname = isset($_POST['lname']) ? $_POST['lname'] : '';
$dob = isset($_POST['dob']) ? $_POST['dob'] : '';

//for example you have submit_button -> named: btnsubmit
//to be clicked if you need to insert it, as follows:

if (isset($_POST['btnsubmit'])){

//then:
if (isset($_POST['btnsubmit'])){

$sql = "INSERT INTO liningdata ( userdata, ipaddress, username,
email, password, passwordc , address, county, country, postcode, title,
fname, lname, dob ) VALUES ( :useragent, :ip, :username, :email,
:password, :passwordc, :address, :county,:country, :postcode, :title,
:fname, :lname, :dob )";
 $query = $db->prepare( $sql );
$query->execute( array( ':useragent'=>$useragent , ':ip'=>$ip ,
':username'=>$username , ':email'=>$email , ':password'=>$password ,
':passwordc'=>$passwordc , ':address'=>$address , ':county'=>$county ,
':country'=>$country , ':postcode'=>$postcode , ':title'=>$title ,
':fname'=>$fname , ':lname'=>$lname , ':dob'=>$dob ) );
if ($query){
echo "success"
}
else{
echo "failed"
}
}