简体   繁体   English

使用PHP添加到WAMP服务器中创建的数据库?

[英]Using PHP to add to a database created in WAMP server?

Ok, so after installing wamp server, I have gone to the phpMyAdmin page and created a database called db2. 好的,所以在安装wamp服务器之后,我进入了phpMyAdmin页面并创建了一个名为db2的数据库。 After that, I have created a table inside of that database called cnt2. 之后,我在该数据库内部创建了一个名为cnt2的表。 It has 5 columns, ID, Name, Mark1, Mark2 and Mark3. 它有5列,ID,名称,Mark1,Mark2和Mark3。 So, I have one html php file that allows you to view the information in the database, and this works just fine. 因此,我有一个HTML php文件,该文件可让您查看数据库中的信息,并且工作正常。 However, my second html php document is supposed to allow you to add new information into the database. 但是,我的第二个html php文档应该允许您将新信息添加到数据库中。 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. 我已经遵循了2个不同的教程,因为我之前从未做过php或任何html脚本,但是它根本无法正常工作。 I'll post both codes/scripts below. 我将在下面发布这两个代码/脚本。

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 :) 谢谢你的帮助 :)

You're not actually requesting your post headers to pull your vars in 您实际上并没有要求您的帖子标头将您的var拉入

<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特别准备好的语句,而不是像上面所做的那样直接将变量注入查询中

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 PDO or at least mysqli . 正如所有其他人所提到的,不要使用mysql_*被弃用的mysql_*函数,而不是坚持使用PDO或至少使用mysqli

Also, the part 另外,这部分

if($submit){

may never be satisfied unless You set the $submit variable 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-使用Google来阅读有关它的信息...

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 ); 创建表用户(id int(11)主键auto_increment,unique_id varchar(23)不为null唯一,名称varchar(50)不为null,电子邮件varchar(100)不为null唯一,encrypted_pa​​ssword varchar(80)不为null,salt varchar( 10)不为null,created_at datetime,updated_at datetime null); /** Creating Users Table **/ / **创建用户表** /

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM