0

在提交数据并插入数据库后,我试图显示用户从 php 程序输入的信息。当我运行它时,我看不到任何结果。我只是得到一个空白页。我在我的代码中找不到错误,希望你们能帮我找到它,这样我就可以修复它并继续前进。这只是我的代码的一部分,我想在我的数据库中插入一些信息,然后显示我插入的内容......

if ( $valid ) { 
    $lines = file('/home/user/Documents/file.txt');
    $uid = trim($lines[0]);
    $pw = trim($lines[1]);
    $dbserver = trim($lines[2]);
    $dbname = trim($lines[3]);

    //Connecting to mysql
    $link = mysqli_connect($dbserver, $uid, $pw, $dbname);
    or die('Could not connect: ' . mysql_error());

    //Our SQL Query
    $firstname = $_POST['firstname'];
    $lastname = $_POST['lastname'];
    $organization = $_POST['organization'];
    $emailaddress = $_POST['emailaddress'];
    $phonenumber = $_POST['phonenumber'];
    $sql_query = "INSERT INTO table VALUES ('$firstname','$lastname','$organization','$emailaddress','$phonenumber')";

        //Run our sql query
    $result = mysqli_query($link, $sql_query) or die('query failed'. mysql_error());

    // Get all records now in DB
    $sql_query = "SELECT * FROM table";
    //Run our sql query
    $result = mysqli_query($link, $sql_query) or die('query failed'. mysql_error());

    //iterate through result printing each record
    echo "<br>Names in DB: <br>";
    while($row = mysqli_fetch_assoc($result)) {
    echo $row['firstname'];
    echo $row['lastname'];
    echo $row['organization']; 
    echo $row['emailaddress'];
    echo $row['phonenumber'];
    echo "<br>";
    }

    // Free resultset (optional)
    mysqli_free_result($result);

    //Close the MySQL Link
    mysqli_close($link);
    }
}
4

2 回答 2

0

更改 $sql_query ="INSERT INTO table VALUES $firstname','$lastname','$organization','$emailaddress','$phonenumber')";

$sql_query = "INSERT INTO table VALUES ($firstname','$lastname','$organization','$emailaddress','$phonenumber')";

此外,您编写的代码非常不安全,很容易受到 SQL 注入的攻击。

花一些时间学习MySQLiPDO insead of Mysql_* 函数。下面是一个使用 mysqli 参数化查询的基本查询:

$query = $mysqli->prepare("SELECT COLUMN_LIST FROM TABLE WHERE COLUMN = ?"); $query->bind_param('s',$UNSAFE_VARIABLE); $查询->执行();

于 2013-02-08T19:48:24.390 回答
0

如果您坚持使用过时的 mysql 函数,那么您正在寻找的是mysqli_insertid 我相信有一个类似的函数。

于 2013-02-08T19:48:27.383 回答