我正在使用WAMP尝试学习一点PHP和SQL。我试图从一个非常基本的表中获取用户输入:
<form action="input.php" method="post" class="registration_form"/>
<fieldset>
<div class="elements">
<label for="name">Username :</label>
<input type="text" id="name" name="name" size="25" />
</div>
<div class="elements">
<label for="e-mail">E-mail :</label>
<input type="text" id="e-mail" name="e-mail" size="25" />
</div>
<div class="elements">
<label for="Password">Password:</label>
<input type="password" id="Password" name="Password" size="25" />
</div>
<div class="submit">
<input type="hidden" name="formsubmitted" value="TRUE" />
<input type="submit" value="Register" />
</div>
</fieldset>
</form>
我希望能够输入并发布到数据库。我一直试图用这段代码来实现这个目标:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
//Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
//Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO MyGuests (name, email, password)
VALUES ($_POST[name], $_POST[e-mail], $_POST[password])";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;//
}
$conn->close();
var_dump('name', 'e-mail', 'password');
?>
当我尝试插入&#34;&#34;如$ _POST [&#34; name&#34;]我收到错误:
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\wamp\www\input.php on line 16
当我尝试删除&#34;&#34;我收到这个错误:
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\wamp\www\input.php on line 16
我还尝试在代码顶部设置变量:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
$name=$_POST['name']
//Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
//Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO MyGuests (name, email, password)
VALUES ('name','email', 'password');
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;//
}
$conn->close();
var_dump('name', 'e-mail', 'password');
?>
这样我最终收到一条错误消息:
( ! ) Parse error: syntax error, unexpected '$conn' (T_VARIABLE) in C:\wamp\www\input.php on line 9
我能够使用$ _POST在另一个脚本中回显该名称,我不知道为什么它不能与SQL命令一起使用。如果有人会帮忙,和/或给我一些资源来学习/学习,我会很感激!
答案 0 :(得分:1)
你遗失了&#39 ;;&#39;后: $名称= $ _ POST [&#39;名称&#39;]
答案 1 :(得分:0)
嗨请注意一些要点
1)您将收到未定义索引的警告,因为您没有检查已发布的值。我们应该在使用值之前使用isset()
。
2)第二次尝试使用lowercase
,只要你给php中的任何标记赋予任何名称,以确保因输入lowercase
或uppercase
而没有错误。
3)如果您的单词很长,请尝试使用underscore
,请使用e_mail
;
因此,您可以使用此php
代码
<?php
if(isset($_POST['register'])){
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
$conn = new mysqli($servername, $username, $password, $dbname);
//Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$name = $_POST['name'];
$email = $_POST['e_mail'];
$password = $_POST['password'];
$sql = "INSERT INTO MyGuests(name, email, password) VALUES('$name','$email','$password')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;//
}
$conn->close();
}
?>
我已将submit
按钮添加到e-mail
改为e_mail
,name="Password"
改为name="password"
<form action="input.php" method="post" class="registration_form"/>
<fieldset>
<div class="elements">
<label for="name">Username :</label>
<input type="text" id="name" name="name" size="25" />
</div>
<div class="elements">
<label for="e-mail">E-mail :</label>
<input type="text" id="e_mail" name="e_mail" size="25" />
</div>
<div class="elements">
<label for="Password">Password:</label>
<input type="password" id="Password" name="password" size="25" />
</div>
<div class="submit">
<input type="hidden" name="formsubmitted" value="TRUE" />
<input type="submit" value="Register" name="register" />
</div>
</fieldset>
</form>