douyan1882 2014-03-25 14:19
浏览 55
已采纳

PHP MySQLi INSERT查询不起作用

Continuing from my previous 2 posts 'PHP and MYSQL database connection and table creation only once' and 'Undefined variable in php registration from', i dont have any errors until I use below code to INSERT the POSTED DATA into my database. As I'm new to MYSQLI I'm not able to understand my errors and debug them. PLZ HELP..

//code for input field validation

if (!empty($error)) //send to Database if there's no error '
    {
    $query = $mysqli->prepare("INSERT INTO users ( Name, Username, Password, Email) VALUES (?, ?, ?, ?, ?)");
    $query ->bind_param('sssdi',$fullname, $username, $password2, $email);
    $stmt->execute();
    $newId = $stmt->insert_id;
    $stmt->close();
    //$mysqli->query($query);
    $result = @mysqli_query($query);
    if (!$result) {
    $error['ack'] = 'Failed to Register Your Account...!';
    } else {
    $error['ack2'] = 'Account Registered Successfully...!';
    }
    //$mysqli->close();//Close the DB Connection

My REGISTRATION.PHP form is available in the 'Undefined variable in php registration from' link.

This is my complete code Memberid)); "; mysql_select_db('USERS'); $retval = mysql_query($sql, $connect); if (!$retval) { die('COULD NOT CREATE TABLE : ' . mysql_error()); } ; mysql_close($connect); //end of DB_connection

$fullname = "";
$username = ""; 
$password = "";
$password2 = "";
$email = "";

if (isset($_POST['submit'])) {
session_start();
$error = array(); //Declare an array to store error messages  

//validation for fullname
if (!empty($_POST['fullname'])) {
    $fullname = mysql_real_escape_string(trim($_POST['fullname']));
} else {
    $error['fullname'] = 'Enter Fullname...';
}

//validation for username
if (!empty($_POST['username'])){
    $username = mysql_real_escape_string(trim($_POST['username']));
} else {
    $error['username'] = 'Enter Username...';
}

//Validation for password and confirm password
if (!empty($_POST['password'])) {
    if ($_POST['password'] != $_POST['password2']) {
        $error['password2'] = 'Passwords do not match...';
    } else {
        $password2 = mysql_real_escape_string($_POST['password']);
    }
    } else {
        $error['password'] = 'Enter Password...';
}

//validation for e-mail
if (!empty($_POST['email'])) {
    $email = mysql_real_escape_string($_POST['email']);
} else {
    $error['email'] = 'Enter your Email...';
} 

if (!empty($error)) //send to Database if there's no error '
{
$query = $mysqli->prepare("INSERT INTO users ( Name, Username, Password, Email) VALUES        (?, ?, ?, ?, ?)");
$query ->bind_param('sssdi',$fullname, $username, $password2, $email);
$stmt->execute();
$newId = $stmt->insert_id;
$stmt->close();
//$mysqli->query($query);
$result = @mysqli_query($query);
if (!$result) {
$error['ack'] = 'Failed to Register Your Account...!';
} else {
$error['ack2'] = 'Account Registered Successfully...!';
}


}//end of main if
?>
<html>
<form action="register.php" method="post" id="user_registration">
<p id="head">Create Account</p>

<input type="text" id="fullname" name="fullname"/>
**<span class="error" id="fullname"><?php echo $error_name; ?></span>**

<input type="text" id="username" name="username"/>
<span id="availability_status"></span>
**<span class="error" id="username"><?php echo $error_username; ?></span>**

<input type="password" id="password" name="password"/> 
**<span class="error" id="password"><?php echo $error_password; ?></span>**

<input type="password" id="password2" name="password2"/>
**<span class="error" id="divCheckPasswordMatch"><?php echo $error_password2;?></span>**

<input type="email" id="email" name="email"/>
**<span class="error" id="email"><?php echo $error_email; ?></span>**

<p class="submit">
<button type="submit"id="submit" name="submit" value="Register”>Register</button>
</p>
  • 写回答

2条回答 默认 最新

  • dsf11t5u1651 2014-03-25 14:30
    关注

    change your code something like this

    $query = $mysqli->prepare("INSERT INTO users (Name, Username, Password, Email) VALUES (?, ?, ?, ?)");
     $query->bind_param("ssss", $fullname, $username, $password2, $email);
     $query->execute();
    

    also double check your field name's are with first capital letter or not, if yes then change the field name as per your table.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据