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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀