dream8877 2013-03-26 05:17
浏览 66
已采纳

尝试将表单数据添加到MYSQL数据库中

I currently have an HTML form with various feilds one for example is :

Please Enter First Name: <input type ="text" name="First_Name" /> <br />

I am trying to get the information from this form into my database. But it does not seem input anything into the database. Code is as follows.

<?php

$dbname='ecig';
$dbhost='localhost';
$dbpass='password';
$dbuser='eciguser';


$dbhandle = mysql_connect($dbhost, $dbuser, $dbpass)
  or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";


//select a database to work with
$selected = mysql_select_db("ecig",$dbhandle)
  or die("Could not select examples");



$res=mysql_query("INSERT INTO Persons (First_Name, Second_Name) VALUES('$_POST[First_Name]', yes)");
mysql_close();

if (array_key_exists ('check_submit', $_POST )) 

echo "Your Name is : {$_POST['First_Name']}<br />";
echo "Your Second Name is : {$_POST['Second_Name']}<br />";
echo "Your Email Address is : {$_POST['Email_Address']}<br />";
echo "Your Password Is : {$_POST['Password']}<br />";

?>

The question is as this is having no impact on my database, is there something i am missing and need to add to my SQL code so that the php and the SQL interact with each other and input the data?

Or am i missing something from the INSERT INTO statement?

Anyway help would be appreciated, Thanks.

  • 写回答

2条回答 默认 最新

  • dougang5088 2013-03-26 05:23
    关注

    There's an error in your query, 'yes' at the end must have quotes.

    Try running your query first directly against MySQL to determine if your syntax is correctly, then just replace the values you want.

    Should look like this for your example:

    $res=mysql_query("INSERT INTO Persons (First_Name, Second_Name) VALUES ('$_POST[First_Name]', 'yes')");
    

    A bit of advice, sanitize your input if you are receiving data from the user, or you will be vulnerable to a SQL injection attack.

    And use mysqli_ functions since mysql_ functions are officially deprecated by now. You can read more about those in almost any site, like w3schools.com

    Full example of insert with mysqli_ : http://www.w3schools.com/php/php_mysql_insert.asp

    Good luck

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测