信息不会在数据库中显示。 XAMPP

时间:2012-12-18 17:44:49

标签: php mysql database xampp connect

我正在尝试创建一个简单的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);    

}   

?>

2 个答案:

答案 0 :(得分: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.");

答案 1 :(得分:0)

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

$QueryResult = mysql_query($SQLsting, $DBConnect);