dpmir1988 2014-03-26 08:38
浏览 33
已采纳

SQL查询失败

I'm trying to insert data into my database and this query is failing and I can't figure out why. I'm just getting the 'Error updating database' output.

Table details

name    varchar(80)      
sitename    varchar(80)      
pages   text         
colors  text         
navigation  text     
content text

PHP

<?php
$name = $_POST['name'];
$sitename = $_POST['sitename'];
$pages = $_POST['pages'];
$color = $_POST['colors'];
$navigation = $_POST['navigation'];
$content = $_POST['content'];
mysql_connect("localhost","csuwebdev","") or die ('Error: ' . mysql_error());
mysql_select_db("csuwebdev");
$query = "INSERT INTO draft (name, sitename, pages, colors, navigation, content) VALUES ( '".$name."',  '".$sitename."', '".$pages."', '".$color."', '".$navigation."', '".$content."')";
mysql_query($query) or die ('Error updating database');
echo "Submission received...";

?>

HTML

<form method="POST" action="processDraft.php">
Your name<br>
<input type="text" name="name"><br>
Name of website<br>
The pages that will comprise the website and a brief description of each. A minimum of 4 is required (Home, About, Contact, and Resume)<br>
<textarea rows="4" cols="100" name="pages">
</textarea><br>
What colors might you use for the background, content background, header text, and paragraph text?<br>
<textarea rows="4" cols="100" name="colors">
</textarea><br>
What will the navigation buttons look like, and where will they be placed?<br>
<textarea rows="4" cols="100" name="navigation">
</textarea><br>
Where will you put your main content for each page?<br>
<textarea rows="4" cols="100" name="content">
</textarea><br>
<input type="submit">
</form>
  • 写回答

1条回答 默认 最新

  • duanmei1350 2014-03-26 08:46
    关注

    Rewrite your code to

    $conn=mysql_connect("localhost","csuwebdev","") or die ('Error: ' . mysql_error());
    mysql_select_db("csuwebdev",$conn);
    

    You need to pass the database connection to your mysql_select_db before making queries to your table.


    This(mysql_*) extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. Switching to PreparedStatements is even more better to ward off SQL Injection attacks !

    PDO Version of the above code. [Didn't test though]

    $dbh =  new PDO('mysql:host=localhost;dbname=csuwebdev', 'username', 'password');
    $stmt = $dbh->prepare("INSERT INTO draft (name, sitename,pages,colors,navigation) VALUES(?,?,?,?,?)");
    $stmt->execute(array($name,$sitename,$pages,$color,$navigation)); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料