使用 PHP 添加到在 WAMP 服务器中创建的数据库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20352098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using PHP to add to a database created in WAMP server?
提问by Daniel Speed
Ok, so after installing wamp server, I have gone to the phpMyAdmin page and created a database called db2. After that, I have created a table inside of that database called cnt2. It has 5 columns, ID, Name, Mark1, Mark2 and Mark3. So, I have one html php file that allows you to view the information in the database, and this works just fine. However, my second html php document is supposed to allow you to add new information into the database. I have followed 2 different tutorials on this as I have never done php or any html script before, but it just isn't working. I'll post both codes/scripts below.
好的,所以在安装 wamp 服务器之后,我转到了 phpMyAdmin 页面并创建了一个名为 db2 的数据库。之后,我在该数据库中创建了一个名为 cnt2 的表。它有 5 列,ID、Name、Mark1、Mark2 和 Mark3。所以,我有一个 html php 文件,它允许您查看数据库中的信息,这很好用。但是,我的第二个 html php 文档应该允许您将新信息添加到数据库中。我已经遵循了 2 个不同的教程,因为我以前从未做过 php 或任何 html 脚本,但它只是不起作用。我将在下面发布两个代码/脚本。
http://gyazo.com/467f8e3a066992c0753eec2d5912bdba<< Database page
http://gyazo.com/467f8e3a066992c0753eec2d5912bdba<< 数据库页面
http://gyazo.com/82a1c2107fb75c4c2941583449b4504a<< Input page with error
http://gyazo.com/82a1c2107fb75c4c2941583449b4504a<< 输入页面错误
Database code
数据库代码
<html>
<body>
<?php
$username = "root";
$password = "";
$hostname = "localhost";
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
$selected = mysql_select_db("db2",$dbhandle)
or die("Could not selected db2");
echo "Coneted to db2<br>", "<br>";
$result = mysql_query("SELECT ID, Name, Mark1, Mark2, Mark3 FROM cnt2");
while($row = mysql_fetch_array($result)){
echo "<b>Name: </b>".$row{'Name'}." <b>ID: </b>".$row{'ID'}." <b>First Mark: </b>".$row{'Mark1'}." <b>Second Mark: </b>".$row{'Mark2'}." <b>Third Mark: </b>".$row{'Mark3'}."<br>";
}
mysql_close($dbhandle);
?>
</body>
</html>
Input code
输入代码
<HTML>
<?php
if($submit){
$db = mysql_connect("localhost", "root","");
mysql_select_db("db",$db);
$sql = "INSERT INTO cnt2 (ID, Name, Mark1, Mark2, Mark3) VALUES ('$id','$name','$markone','$marktwo','$markthree','$result = mysql_query($sql))";
echo "Thanks! Data received and entered.\n";
}
else{
?>
<form method="post" action="datain.php">
id:<input type="Int" name="ID"><br>
name:<input type="Text" name="Name"><br>
markone:<input type="Int" name="Mark1"><br>
marktwo:<input type="Int" name="Mark2"><br>
markthree:<input type="Int" name="Mark3"><br>
<input type="Submit" name="submit" value="Enter information">
</form>
<?
}
?>
</HTML>
Thanks for any help :)
谢谢你的帮助 :)
回答by Dave
You're not actually requesting your post headers to pull your vars in
您实际上并没有要求您的帖子标题将您的变量拉入
<html>
<?php
if($submit){
//need to request post vars here
$id=mysql_real_escape_string($_POST['ID']);
$name=mysql_real_escape_string($_POST['Name']);
$markone=mysql_real_escape_string($_POST['Mark1']);
$marktwo=mysql_real_escape_string($_POST['Mark2']);
$markthree=mysql_real_escape_string($_POST['Mark3']);
$db = mysql_connect("localhost", "root","");
mysql_select_db("db",$db);
$sql = "INSERT INTO cnt2 (ID, Name, Mark1, Mark2, Mark3) VALUES ('$id','$name','$markone','$marktwo','$markthree')";
mysql_query($sql) or die(mysql_error()."<br />".$sql);
echo "Thanks! Data received and entered.\n";
}
else{
?>
<form method="post" action="datain.php">
id:<input type="Int" name="ID"><br>
name:<input type="Text" name="Name"><br>
markone:<input type="Int" name="Mark1"><br>
marktwo:<input type="Int" name="Mark2"><br>
markthree:<input type="Int" name="Mark3"><br>
<input type="Submit" name="submit" value="Enter information">
</form>
<?php // stop using short tags i've swapped it to a proper open
}
?>
</html>
Also if you're only just using don't use mysql_ functions look into mysqli or pdo especially prepared statements instead of directly injecting variables into queries as we have done above
此外,如果您只是使用,请不要使用 mysql_ 函数查看 mysqli 或 pdo 特别准备好的语句,而不是像我们上面所做的那样直接将变量注入查询中
回答by shadyyx
The problem may be in this line:
问题可能出在这一行:
$sql = "INSERT INTO cnt2 (ID, Name, Mark1, Mark2, Mark3) VALUES ('$id','$name','$markone','$marktwo','$markthree','$result = mysql_query($sql))";
As You may notice (at the end), it should probably be like this:
您可能会注意到(最后),它可能应该是这样的:
$sql = "INSERT INTO cnt2 (ID, Name, Mark1, Mark2, Mark3) VALUES ('$id','$name','$markone','$marktwo','$markthree')";
$result = mysql_query($sql);
As all other people mentioned, do not use mysql_*functions as they are DEPRECATED, instead of this stick with PDOor at least mysqli.
正如所有其他人所提到的,不要使用mysql_*已弃用的函数,而不要使用PDO或至少mysqli。
Also, the part
此外,该部分
if($submit){
may never be satisfied unless You set the $submitvariable somewhere before... Shouldn't it rather be
除非您$submit之前在某处设置变量,否则可能永远不会满足...
if (isset($_POST['submit'])) {
???
???
And, please, read about code formatting- Your code looks like crap... Best choice is to stick with PSR-0, PSR-1 and PSR-3 - use Google to read something about it...
而且,请阅读代码格式- 你的代码看起来很垃圾......最好的选择是坚持使用 PSR-0、PSR-1 和 PSR-3 - 使用谷歌阅读有关它的内容......
回答by omkar more
create database android_api /** Creating Database **/
创建数据库 android_api /** 创建数据库 **/
use android_api /** Selecting Database **/
使用 android_api /** 选择数据库 **/
create table users( id int(11) primary key auto_increment, unique_id varchar(23) not null unique, name varchar(50) not null, email varchar(100) not null unique, encrypted_password varchar(80) not null, salt varchar(10) not null, created_at datetime, updated_at datetime null ); /** Creating Users Table **/
创建表用户(id int(11)主键自动增量,unique_id varchar(23)非空唯一,名称varchar(50)非空,电子邮件varchar(100)非空唯一,encrypted_password varchar(80)非空,salt varchar( 10) not null, created_at datetime, updated_at datetime null ); /** 创建用户表 **/

