0

我正在尝试制作一个简单的 RSVP 程序,用户只需输入他们的名字和姓氏,然后单击是或否。然后将信息带入数据库。我被困在用户可以输入名字和姓氏并单击“是”或“否”的位置,但它没有显示在数据库中。这是我到目前为止所拥有的。任何帮助将不胜感激。我仍然是 php 的菜鸟。我也在为我的数据库使用 xampp。谢谢

这是上半场

<H1><div align="center">RSVP</div></H1>

<H3>Enter in the information that is requried</H3>

<form method ="POST" action = "RSVP.php">

Please type in your first name</br>

<input type = "text" name="fname"/></br>

Please type in your last name</br>

<input type = "text" name="lname"/>

</br></br></br></br>

<H2>Will you be attending?</h2>

<input type="radio" name="yorn" value="Yes">Yes

<input type="radio" name="yorn" value="No">No</br>


<input type="submit" value="Submit">

</form>

这是php的一半

<?php

if(empty($_POST['fname']) || empty($_POST['lname']))

print "Please type in BOTH first name and last name";
else{

$DBConnect = @mysql_connect("localhost", "Jordan", "bigboy");

if ($DBConnect === FALSE){

    print "<p>Unable to connect to the database server.<p>". "<p>Error code " 
                .mysql_errno(). ": ". mysql_error() . "</p>";

}else{

    $DBName = "jdatabase";

    mysql_select_db("jdatabase") or die(mysql_error());

    $TableName = "RSVP";

    $firstname = $_POST['fname'];

    $lastname = $_POST['lname'];

    $YorN = $_POST['yorn'];

    $SQLsting = "INSERT INTO '$TableName' VALUES(NULL, 

                   '$firstname','$lastname','$YorN')";

$QueryResult = @mysql_connect($SQLsting, $DBConnect);

}if ($QueryResult === FALSE){

    print "<p>Unable to execute query.<p>". "<p>Error code " 
                .mysql_errno($DBConnect). ": ". mysql_error($DBConnect) . "</p>";

}else{

    print "Thank You for RSVP";

}

mysql_close($DBConnect);    

}   

?>
4

2 回答 2

1

这是输入转义的基本 mysqli 用法。

$DBName = "jdatabase";
$TableName = "RSVP";
$firstname = $_POST['fname'];
$lastname = $_POST['lname'];
$YorN = $_POST['yorn'];

/* Connection info for PHP/MySQL test database. */
defined('DB_HOST') ? NULL : define('DB_HOST', 'localhost');
defined('DB_USER') ? NULL : define('DB_USER', 'testUzer');
defined('DB_PWD') ? NULL : define('DB_PWD', 'xYzT@9123');
defined('DB_NAME') ? NULL : define('DB_NAME', $DBName);

/* Connect to database */
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PWD, DB_NAME);
if ($mysqli->connect_errno) exit("ERROR: Could not connect to database.");

/* Build the query string and escape input. */
$query = sprintf("INSERT INTO $TableName (first_name, last_name, yorn) VALUES('%s', '%s', '%s')", 
$mysqli->real_escape_string($firstname), 
$mysqli->real_escape_string($lastname), 
$mysqli->real_escape_string($YorN));

/* Insert the record. */
$result = $mysqli->query($query);
if(! $result) exit("ERROR: Database query failed.");
于 2012-12-18T18:05:54.807 回答
0

即使您要使用旧的 mysql_* 方式,您也不会查询数据库...您应该调用 mysql_query... 在您的代码中您使用 mysql_connect 两次。由于前面有 @ 符号,您不会因此收到错误消息。

$QueryResult = mysql_query($SQLsting, $DBConnect);
于 2012-12-18T18:13:32.110 回答