0

我在使用表单在我的数据库中发布数据时遇到问题。我确定非常基本的东西,但我很坚持:/

我可以使用 select from database 例程从我的数据库中取出东西。所以我知道与数据库的连接可能不是问题。

这是我的'upload.php':

<html>
<head>
<title>Upload</title>
</head>
<body>

    <form action="action.php" method="post">

        <fieldset class="first">
             Name: 
        <input type="text" name="author" />
        Heading:
          <input type="text" name="heading" />

        Text:
        <textarea type="text" name="thecontent"> </textarea>
        </fieldset>


        <fieldset>
        <input type="submit"/> 
            </fieldset>
    </form>



</body>
</html>

这是我的“action.php”:

<html>
<head>

<title>Send!</title>
</head>

<body>

<?php

 ini_set('display_errors', 1); error_reporting(E_ALL); 

$link = mysql_connect('localhost','name','pasword')
or die ("Unable to connect"); 

$mydb = mysql_select_db('the_database',$link)
or die ("No database found"); 

$author = $_POST['author']; 
$heading = $_POST['heading'];
$thecontent = $_POST['thecontent'];

$mysql_query="INSERT INTO articles ('heading', 'author', 'content')
 VALUES ('$heading','$author','$thecontent')" or die(mysql_error());   

echo "This was send: $author $heading $thecontent <br> ";


 mysql_close()

?> 

</body>
</html>

所有帮助将不胜感激!

干杯,齐格

感谢所有的帮助家伙!我正在尝试使用 mysqli 插入数据但是它还没有工作这是我在 action.php 中的新代码:

<html>
<head>

<title>Send!</title>
</head>

<body>

<?php

 ini_set('display_errors', 1); error_reporting(E_ALL); 
$DB_HOST = 'localhost';
$DB_USER = '**';
$DB_PASS = '***';
$DB_NAME = '***';
@ $db = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if (mysqli_connect_errno()) {
echo 'Error.';
exit();
}

$author = $_POST['author']; 
$heading = $_POST['heading'];
$thecontent = $_POST['thecontent'];

$query = 'INSERT INTO articles ('heading', 'author', 'content')
 VALUES ('$heading','$author','$thecontent')';   

$result = $db->query($query);
    if ($result) {
    echo $db->affected_rows."This was added.";
    } 
    else {
    echo "somethings gone very wrong.";
    }

$db->close();




?> 

</body>
</html>

我做错了什么伙计们?非常感谢您的帮助!

干杯,齐格

4

2 回答 2

1

您构建了INSERT字符串,但您从不调用一个方法来用它来真正地插入数据库。

此外,不推荐使用旧mysql_*方法,使用PDOAPImysqli代替,请参阅http://www.php.net/manual/en/mysqlinfo.api.choosing.php

另请参阅有关此内容的 stackoverflow 帖子:mysqli 或 PDO - 有什么优点和缺点?

一些PDO准备好的语句示例PDOhttp ://www.php.net/manual/en/pdo.prepare.php

于 2012-10-01T19:53:31.680 回答
-1
$mysql_query="INSERT INTO articles    ('heading', 'author', 'content')
 VALUES ('$heading','$author','$thecontent')";
mysql_query($mysql_query);
//required to run the query..

And mysql_close(); // missing :p
于 2012-10-01T20:01:06.770 回答