dpzbzp8728 2016-09-29 19:36
浏览 46
已采纳

数据从数据库中检索但不会插入?

So when I want to retrieve data and check it i.e. if the email already exist echo already registered. That part works fine, however inserting the same data does not work. Are my conditionals ordered improperly? (intentionally left out values for the dbhostname id pw variables)

$dbname = "hw2";

$link = mysqli_connect($dbhostname, $dbuserid, $dbpassword, $dbname);

$firstname = $_POST["signup-firstname"];
$lastname = $_POST["signup-lastname"];
$email = $_POST["signup-email"];
$password = $_POST["signup-password"];
$repassword = $_POST["signup-repassword"];

if ($password != $repassword){
    echo "<br><h3>Passwords did not match. <br>Please try again.</h3>";
}

else {

$ret_email = "SELECT * FROM hw2 WHERE email = '$email'";
$result = mysqli_query($link, $ret_email);
$num_rows = mysqli_num_rows($result);

    if ($num_rows > 0){
    echo "This email is already registered.";
    }   

    else{
    $insert_query = "INSERT INTO hw2 (firstname, lastname, email, password, repassword) VALUES ('$firstname', '$lastname', '$email', '$password', '$repassword')";
    echo "$insert_query";
    }       
}
?>
  • 写回答

1条回答 默认 最新

  • dqyl2374 2016-09-29 19:40
    关注

    You should perform the query not only echoing it

    mysqli_query($con,"INSERT INTO Persons (FirstName,LastName,Age) 
    
    if ($num_rows > 0){
    echo "This email is already registered.";
    }   
    
    else{
    $insert_query = "INSERT INTO hw2 (firstname, lastname, email, password, repassword) VALUES ('$firstname', '$lastname', '$email', '$password', '$repassword')";
    
    echo "$insert_query";
    mysqli_query($link,$insert_query)
    }   
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 WPF动态创建页面内容
  • ¥15 如何对TBSS的结果进行统计学的分析已完成置换检验,如何在最终的TBSS输出结果提取除具体值及如何做进一步相关性分析
  • ¥15 SQL数据库操作问题
  • ¥100 关于lm339比较电路出现的问题
  • ¥15 Matlab安装yalmip和cplex功能安装失败
  • ¥15 加装宝马安卓中控改变开机画面
  • ¥15 STK安装问题问问大家,这种情况应该怎么办
  • ¥15 关于罗技鼠标宏lua文件的问题
  • ¥15 halcon ocr mlp 识别问题
  • ¥15 已知曲线满足正余弦函数,根据其峰值,还原出整条曲线