如何连接到指向 PHP 代码的 localhost/html 表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19342098/
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
How to connect to localhost/ html form directing to PHP code
提问by AfroBiscuit
I'm trying to figure out how to connect to my localhost in order to run some php updating a local mysql server. Below is my html code containing the form, and the php im running. When i submit my form, it directs me to my php file in browser (as code). Is that supposed to happen? On top of that, my SQL database is not being updated.
我想弄清楚如何连接到我的本地主机以运行一些 php 更新本地 mysql 服务器。下面是我的包含表单的 html 代码,以及正在运行的 php。当我提交表单时,它会将我定向到浏览器中的 php 文件(作为代码)。这是应该发生的吗?最重要的是,我的 SQL 数据库没有更新。
test_2232.html:
test_2232.html:
<html>
<body>
<form action="update_2232.php" method="post">
<h3>Code:</h3>
<input type="text" name="officeCode">
<h3>Name:</h3>
<input type="text" name="name">
<input type="submit">
</form>
</body>
</html>
update_2232.php:
update_2232.php:
<?php
//Connecting to sql db.
mysql_connect('localhost', 'mysql_user', 'mysql_password') or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
//Sending form data to sql db.
mysqli_query($connect,"INSERT INTO office (officeCode, name)
VALUES ('601', 'Boora')");
?>
Thanks for any tips
感谢您的任何提示
回答by Sonya Krishna
Try this code
试试这个代码
<?php
$officecode=$_POST['officeCode'];
$name=$_POST['name'];
mysql_connect('localhost', 'mysql_user', 'mysql_password') or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
mysql_query($connect,"INSERT INTO office (officeCode, name)
VALUES ('$officecode', '$name')");
?>
OR
或者
<?php
$officecode=$_POST['officeCode'];
$name=$_POST['name'];
mysqli_connect('localhost', 'mysql_user', 'mysql_password') or die(mysqli_error());
mysqli_select_db("test") or die(mysql_error());
mysqli_query($connect,"INSERT INTO office (officeCode, name)
VALUES ('$officecode', '$name')");
?>
You can't use both mysql and mysqli together.
您不能同时使用 mysql 和 mysqli。