[英]My PHP file is not posting form to the database
在这里输入代码我的php文件没有在我的数据库中发布表单。 我在其他线程中进行了搜索,但无法使其正常工作。 我尝试了所有内容,但无法正常运行。 我正在使用XAMPP进行托管。 码:
<?php
$link=mysql_connect('localhost','root','');
mysql_select_DB('registro',$link);
?>
<html>
<head>
<meta charset="utf-8" />
<link rel="icon" type="image/png" href="/images/icon.png" />
<title>Insert.</title>
<link href="estilo.css" rel="stylesheet" type="text/css" />
</head>
<link href="../../../../../Mis Cosas/Mis documentos/Insert/estilos/estilos.css" rel="stylesheet" type="text/css">
<body>
<?php if(!$_POST) { ?>
<h1>Registro</h1>
<form id="form1" name="form1" action="index.php" method="POST">
<p>Nombre:
<input name="nombre" type="name" class="input" placeholder="Nombre">
</p>
<p><BR>Apellido:
<input name="ape" type="text" class="input" placeholder="Apellido">
</p>
<p>Email:
<input name="email" type="email" class="input" placeholder="Email">
</p>
<p>Teléfono:
<input name="tel" type="text" class="input" placeholder="Teléfono">
<BR>
<input name="enviar" type="submit" class="button" value="Enviar">
</p>
</form>
<?php
}else{
$nombre=$_POST['nombre'];
$apellido=$_POST['ape'];
$email=$_POST['email'];
$tel=$_POST['tel'];
$sql = "INSERT INTO clientes(nombre,apellido,email,tel) VALUES('$nombre','$apellido','$email','$tel')";
Mysql_query($sql);
print"Done";
}?>
</body>
</html>
第一个问题:
Mysql_query($sql); //even though php functions are case-insensitive
^
should be lower case
第二个问题:您没有在转义数据,应该使用PDO或MySQLi。 这给您的网站带来了严重的问题,例如SQL Injection。
我认为您的问题是您没有在逃避变量。 如果任何输入字段都包含引号,则可能会收到like syntax error
的错误消息,但是由于error_reporting
它可能会显示在error_log
。 所以要么使用mysql_real_escape_string($postVariable);
或使用准备好的语句。
PDO示例:
$nombre = $_POST['nombre'];
$apellido = $_POST['ape'];
$email = $_POST['email'];
$tel = $_POST['tel'];
$sql = "INSERT INTO clientes(nombre, apellido, email, tel)
VALUES(:nombre, :apellido, :email, :tel)";
$stmt = $pdoObj->prepare($sql);
$stmt->bindParam(':nombre', $nombre, \PDO::PARAM_STR);
$stmt->bindParam(':apellido', $apellido, \PDO::PARAM_STR);
$stmt->bindParam(':email', $email, \PDO::PARAM_STR);
$stmt->bindParam(':tel', $tel, \PDO::PARAM_STR);
if ($stmt->execute()) {
echo 'DONE';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.