简体   繁体   English

PHP / MySQL:将值保存到数据库

[英]PHP/MySQL: Saving values to database

Greetings good people, 问候好人,

I am a rookie at coding using php/mysql and as I was working through a basic task I hit a snag when it came to saving the values of text-fields and checkboxes states to a mysql DB. 我是使用php / mysql进行编码的新手,在完成基本任务时,遇到了将文本字段和复选框状态的值保存到mysql DB的问题。

Here is what I was able to do but I still couldn't save a record in the DB. 这是我能够执行的操作,但仍然无法在数据库中保存记录。

WHAT AM I DOING WRONG OR WHAT AM I MISSING? 我在做错什么还是我在做错什么?

Am using Macromedia Dreamweaver 8, Apache/2.2.12 (Win32) DAV/2 mod_ssl/2.2.12 OpenSSL/0.9.8k mod_autoindex_color PHP/5.3.0 mod_perl/2.0.4 Perl/v5.10.0 MySQL client version: 5.1.37 PHP extension: mysqli 我正在使用Macromedia Dreamweaver 8,Apache / 2.2.12(Win32)DAV / 2 mod_ssl / 2.2.12 OpenSSL / 0.9.8k mod_autoindex_color PHP / 5.3.0 mod_perl / 2.0.4 Perl / v5.10.0 MySQL客户端版本:5.1.37 PHP扩展名:mysqli

$con = mysqli_connect("localhost", "root", "");
if (!$con) {
exit('Connect Error (' . mysqli_connect_errno() . ') '
       . mysqli_connect_error());
}


if ($_POST['textfield'] == "") {
        $FieldsEmpty=  true;
    } 
     else {

       if ($FieldsEmpty) echo "Please enter all the fields<br/>";
    }


if($_POST['Submit2'] == "Submit")
{


$VarName = $_POST['textfield'];
$VarOrg = $_POST['textfield2'];
$VarAddress = $_POST['textfield3'];
$VarPhone=$_POST ['textfield4'];
$VarEmail=$_POST['textfield5']; 
$VarAccomodation=$_POST['checkbox'];
$VarEntertainment=$_POST['checkbox2'];
$VarTourOP=$_POST['checkbox3'];
$VarDomesticTourism=$_POST['checkbox4'];
$VarTourism=$_POST['checkbox5'];
$VarTravelmgt=$_POST['checkbox6'];
$VarSupport=$_POST['checkbox7'];
$VarMedia=$_POST['checkbox8'];
$VarDoc1=$_POST[$_FILES["file"]["name"]];
$VarDoc2=$_POST[$_FILES["file2"]["name"]];
$VarDoc3=$_POST[$_FILES["file3"]["name"]];
$Vardoc4=$_POST[$_FILES["file4"]["name"]];



 $sql = "Insert into nominatons_tbl(name,org,address,phone,email,best_accomodation,best_entertainment,best_touroperator,best_domestictourism,best_tourism,best_travelmgt,best_support,best_media,doc1,doc2,doc3,doc4) VALUES (".PrepSQL($VarName) . ", " .PrepSQL($VarOrg) . ", " .PrepSQL($VarAddress) . ", " .PrepSQL($VarPhone) . ", " .PrepSQL($VarEmail) . ", " .PrepSQL($VarAccomodation) . ", " .PrepSQL($VarEntertainment) . ", " .PrepSQL($VarTourOP) . ", " .PrepSQL($VarDomesticTourism) . ", " .PrepSQL($VarTourism) . ", " .PrepSQL($VarTravelmgt) . ", " .PrepSQL($VarSupport) . ", " .PrepSQL($VarMedia) . ", " .PrepSQL($VarDoc1) . ", " .PrepSQL($VarDoc2) . ", " .PrepSQL($VarDoc3) . ", " .PrepSQL($Vardoc4) . ")";

mysql_query($sql);
echo "Nomination submited <br />";

}
function PrepSQL($value)
{
// Stripslashes
if(get_magic_quotes_gpc())
{
    $value = stripslashes($value);
}
// Quote
$value = "'" . mysql_real_escape_string($value) . "'";
return($value);

} }

keep the single quotes like this 保持这样的单引号

VALUES ('".PrepSQL($VarName) . "', '" .PrepSQL($VarOrg) . "'); VALUES('“ .PrepSQL($ VarName)。”','“ .PrepSQL($ VarOrg)。”');

  • Please check if you are getting all values in $_POST variable. 请检查是否在$ _POST变量中获取所有值。
  • Anytime you get error in insert query , just run query from database , you will get whats problem. 每当您在插入查询中遇到错误时,只要从数据库中运行查询,您都会遇到问题。

Always keep practice to make query like this. 始终保持练习进行这样的查询。

$sql = "Insert into nominatons_tbl(`name`,`org`,`address`,`phone`,`email`,`best_accomodation`,`best_entertainment`,`best_touroperator`,`best_domestictourism`,`best_tourism`,`best_travelmgt`,`best_support`,`best_media`,`doc1`,`doc2`,`doc3`,`doc4`) VALUES 
 (".PrepSQL($VarName) . ", " .PrepSQL($VarOrg) . ", " .PrepSQL($VarAddress) . ", " .PrepSQL($VarPhone) . ", " .PrepSQL($VarEmail) . ", " .PrepSQL($VarAccomodation) . ", " .PrepSQL($VarEntertainment) . ", " .PrepSQL($VarTourOP) . ", " .PrepSQL($VarDomesticTourism) . ", " .PrepSQL($VarTourism) . ", " .PrepSQL($VarTravelmgt) . ", " .PrepSQL($VarSupport) . ", " .PrepSQL($VarMedia) . ", " .PrepSQL($VarDoc1) . ", " .PrepSQL($VarDoc2) . ", " .PrepSQL($VarDoc3) . ", " .PrepSQL($Vardoc4) . ")";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM