我正在尝试使用PHP连接到MYSQL数据库并将信息插入表中来创建注册和登录页面。不幸的是,我能够毫无问题地连接到数据库,并且它连接到表。当我通过HTML表单提交信息来处理信息时,它表示已添加一个条目,但MYSQL数据库有一个空白条目。我将在下面发布HTML和PHP。谢谢你的帮助。
<HTML>
<HEAD>
<TITLE> Programming </TITLE>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<LINK REL="stylesheet" TYPE="text/css" href="homework2.css">
</HEAD>
<BODY>
<!-- CSS for http://the7.dream-demo.com/ -->
<div id="container">
<div id="header">
<div class="menuitem"> <a href="home.html">Home</a> </div>
<div class="menuitem"><a href="products.html">Products</a></div>
<div class="menuitem"><a href="cases.html">Case Studies</a></div>
<div class="menuitem"><a href="pricing.html">Pricing</a></div>
<div class="menuitem"><a href="aboutus.html">About Us</a></div>
</div>
<div id="bodycontent">
<div id="banner">
<div id="bannerleft"> <h1> We make you better athletes. Find out how! </h1> </div>
<div id="signin">
<form class="well form-inline" action="login.php" method="post">
<input type="text" class="input-small" placeholder="Email" name="email" >
<input type="password" class="input-small" placeholder="Password" name="password">
<br><br>
<!--
If you do not want to use twitter bootstrap css then you should uncomment next 6 lines and uncomment the
above 2 lines that provide input boxes
<label for="email">Email:</label>
<input type="text" name="email" id="email">
<br>
<label for="password">Password:</label>
<input type="password" name="password" id="password">
<br>
-->
<input type="submit" name="submit" id="logmein" value="Log In">
</form>
</div>
</div>
<div id="featurestrip">
<div id="signup">
<form action="signup.php" method="post">
<label for="user_name">Firstname:</label>
<input type="text" name="signup-FirstName" id="signup-FirstName">
<br>
<label for="user_pass">Password:</label>
<input type="password" name="signup-Password" id="signup-Password">
<br><br>
<label for="user_email">Email: </label>
<input type="text" name="signup-email" id="signup-email">
<br>
<input type="submit" name="signmeup" id="signmeup" value="Sign Me Up!">
</form>
</div>
<div id="featureright"> <p>Sign up and find out more on how we can help. Pricing starts at $19.95 a month. </p>
<p><h3>Premium service starts at $49.95.</h3></p>
</div>
</div>
<div id="corefeatures">
<img height="200px" src="http://www.hockeymanitoba.ca/wp-content/uploads/2013/02/ltad-model.jpg">
</div>
<div id="testimonials"> Testimonial
<img height="200px" src="http://www.neuroexplosion.com/storage/development%20model%20jpeg.jpg?__SQUARESPACE_CACHEVERSION=1305662626397">
<img height="200px" src="http://www.phecanada.ca/sites/default/files/physical_literacy/LTAD_FMS.jpg">
</div>
<!--
<div id="portfolio"> Portfolio</div>
<div id="skills"> Skills</div>
-->
</div>
<div id="footer">Copyright Notice. All Rights Reserved. 2014</div>
</div>
</BODY>
</HTML>
PHP
以下PHP代码
<?php
echo "<TR>";
echo "<TD>";
$dyn_user_name = $_POST['user_name'];
echo $dyn_user_name;
echo "</TD>";
echo "<TD>";
$dyn_user_pass = $_POST['user_pass'];
echo $dyn_user_pass;
echo "</TD>";
echo "<TD>";
$dyn_user_email= $_POST['user_email'];
echo $dyn_user_email;
echo "</TD>";
echo "</TR>";
///This is for connecting to the database
$mysql_hostname = 'localhost';
$mysql_user = 'username';
$mysql_password = 'password';
$mysql_database = 'users_db2015';
$connect = mysql_connect($mysql_hostname, $mysql_user, $mysql_password)
or die ("Couldn't connect");
echo "Connection Successful";
//to put data into database
//select database
$db_selected = mysql_select_db($mysql_database, $connect)
or die ("Couldn't connect to the database");
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
echo "<BR>Selected the database " . $mysql_database;
//create sql query for data insertion
$insert_sql = "INSERT INTO users1 (user_name, user_pass, user_email)
values ('$dyn_user_name', '$dyn_user_pass', '$dyn_user_email')";
$insert_result = mysql_query($insert_sql);
//if successful, add another row, if not, then don't
if ($insert_result){
echo "<BR> 1 record added";
}
else{
echo "<BR> Couldn't add the information";
}
//Display data in the database (run a query in mysql to retrieve data)
//prepare query
$get_data_sql = "SELECT * FROM users1";
//run query
$result= mysql_query($get_data_sql);
//get numbner of rows in the result
$num_rows = mysql_num_rows($result);
//loop through result
$q=0;
while ($q < $num_rows){
//takes results and gives you the first row, which is
//stored as a hashtable
//takes row and represents as key value pair, keep on using the value pair
//as the keys
$new_row= mysql_fetch_array($result);
//look into firstname and retrieve what's in that row, so it will return a name
//do this same this for the score
echo $row_user_name = $new_row['user_name'];
echo $row_user_pass = $new_row['user_pass'];
echo $row_user_email = $new_row['user_email'];
echo "<TR>";
echo "<TD>";
echo $row_user_name;
echo "</TD>";
echo "<TD>";
echo $row_user_pass;
echo "</TD>";
echo "<TD>";
echo $row_user_email;
echo "</TD>";
echo "</TR>";
$q=$q+1;
}
?>