doucai6663 2013-11-27 14:00
浏览 18

使用SQL进行PHP验证

Hi I'm brand new to PHP and I'm just doing a simple form to learn. It just contains an email address, I want to Validate the information and send it to the database. My problem is connecting it to a database. I've done a few tutorials but just leave myself confused.

Right now this is my homepage

reg.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Registration Form</title>
</head>

<body>
<?php include("validation.php"); ?>
<form method="post" action="connect.php" name="form">
<ul id="errors">
    <li><?php echo $err_email; ?></li>
</ul>   
<div id="wrapper">
<div>Email</div>
<div class="input"><input type="text" name="email" value="<?php echo $val_email; ?>" /></div>


</div>
</form>
</body>
</html>

This is my validation.php file

<?php
if($_POST)
{
    $email = $_POST['email']; 

    // Email
    if (preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/', $email)) {
        $val_email = $email; 
    }else{ 
        $err_email = 'Please enter valid Email address.'; 
    }

    if((strlen($val_email)>0) ){
        header("Location: reg");
    }else{ }
}
?>

finally my connect file

 <?php

$host="localhost";
$username="admin";
$password=""; // Mysql password
$db_name="davidtest"; // Database name
$tbl_name="users"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("davidtest")or die("cannot select DB");

// Get values from form
$email=$_POST['email'];

// Insert data into mysql
$sql="INSERT INTO $users(email)VALUES('$email')";
$result=mysql_query($sql);

// close connection
mysql_close();
?>
  • 写回答

1条回答 默认 最新

  • doumengxue7371 2013-11-27 14:06
    关注
    INSERT INTO $users(email)VALUES('$email')
    

    $users isn't a variable so will be empty when being placed into the table. I suspect you meant

    $sql="INSERT INTO $tbl_name (email) VALUES ('$email')";
    

    However you should never use unescaped user strings in queries since a malicious user could inject SQL into your query (read up on SQL Injection).

    Also please be aware that the mysql_* series of functions is now deprecated, no longer maintained and will be removed in a future release of PHP. Consider mysqli or PDO.

    评论

报告相同问题?

悬赏问题

  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀