简体   繁体   English

数据未从PHP输入数据库

[英]Data not entered into database from PHP

I am trying to enter data into a database with PHP. 我正在尝试使用PHP将数据输入数据库。

Here is my code: 这是我的代码:

<?php
    $username = 'username'; //username for database
    $password = 'password'; //password for database
    $hostname = 'localhost'; //host
    $db_name =  'db_testdrubin'; //name of database

    $db_selected = mysqli_connect($hostname, $username, $password, $db_name)//specify database
    or die ("unable to connect");

    if(isset ($_POST['submit'])){
        $ID = ($_POST['ID']);
        $fname = ($_POST['fname']);
        $lname = ($_POST['lname']);
        $address = ($_POST['address']);
        $city = ($_POST['city']);
        $state = ($_POST['state']);
        $zip = ($_POST['zip']);
        $phone = ($_POST['phone']);
        $email = ($_POST['email']);
        $books = ($_POST['books[]']);
        $comments = ($_POST['comments']);
    }
    else{
        echo'<p>not submitted</p>';
    }
    //up until this point the code works fine

    $query = 'INSERT INTO Student VALUES ($ID, $fname, $lname, $address, $city, $state, $zip, $phone, $email, $books, $comments)';


    $success = $db_selected->query($query);

    if($success){
        $count = $db_selected->affectd_rows;
        echo '<p>$count were added</p>';
    }
    else{
        echo '<p>error</p>';
    }

?>

I know that the information is being read from the html form correctly because I have checked by printing the individual variables. 我知道信息是从html表单正确读取的,因为我已经通过打印各个变量进行了检查。 I am not getting any error messages when I submit the form, just the "error" echo statement from the if/else statement, and no data is entered into the database. 提交表单时,没有收到任何错误消息,只是if / else语句中的“错误” echo语句,并且没有任何数据输入数据库。

I have also tried this: 我也尝试过这个:

if (!mysql_query($db_selected, $query)){
    echo '<p>error</p>';
}

with the same results. 具有相同的结果。

Change this 改变这个

$query = 'INSERT INTO Student VALUES ($ID, $fname, $lname, $address, $city, $state, $zip, $phone, $email, $books, $comments)';

to

$query = "INSERT INTO Student VALUES ($ID, '$fname', '$lname', '$address', '$city', '$state', $zip, $phone, '$email', '$books', '$comments')";

I mean to say if its string then do like '$string' and also use 我的意思是说它的字符串是否喜欢'$ string'并使用

$db_selected->real_escape_string($stringval);

and use 和使用

echo $db_selected->error;

to check the error you got. 检查您得到的错误。

$ins="insert into Student (`id`,`fname`,`lname`,`address`,`city`,`state`,`zip`,`phone`,`email`,`books`,`comments`)values
  ('".$ID."','".$fname."','".$lname."','".$address."','".$city."','".$state."','".$zip."','".$phone."','".$email."','".$books."','".$comments."')"; 

mysql_query($ins); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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