douduiyun6125 2014-09-26 21:45
浏览 65
已采纳

我无法使用HTML表单和PHP将记录插入数据库

No records show up in my MyPHP Database when I run these scripts.

I am running: APACHE 2.4.7 MYSQL 5.6.15 PHP 5.5.8

First the HTML Code...

<html>

<center>
  <font face="Helvetica">

<u><b>Matthew Gieger's Guestbook</b></u>

<form action="link.php" method="post"/>
<p>Name: </p>
<input type="text" name="Name" required/>
<p>Email: </p>
<input type="email" name="Email" required />
<p>Message: </p>
<p><textarea rows="4" cols="50" name="Message"> </textarea></p>
<input type="submit" value="Submit" />
<input type="reset" value="Reset" />
</form>
</center>
</html>

And the PHP Script. This is where I think the problem is...

 <?php

$username='root';
$password='';
$database='guestbook';

$name= $_POST['Name'];
$email= $_POST['Email'];
$message= $_POST['Message'];

new mysqli('localhost',$username,$password,$database) or die("could not connect to localhost");

echo"connected";

mysqli:"insert into contacts (Name,Email,Message,Timestamp) values ($name,$email,$message,date())";

?>

I get no error when I run the code. I just get the expected "connected"

  • 写回答

3条回答 默认 最新

  • doufubian3479 2014-09-26 21:55
    关注

    You don't have a mysqli object defined anywhere. And you're not using mysqli_query() anywhere.

    Here are the docs to mysqli.

    Try something like this:

    $mysqli = new mysqli('localhost', $username, $password, $database);
    
    /* check connection */
    if ($mysqli->connect_errno) {
        printf("Connect failed: %s
    ", $mysqli->connect_error);
        exit();
    }
    
    $mysqli->query("INSERT INTO `contacts`(`Name`, `Email`, `Message`, `Timestamp`) VALUES ('". $name ."', '". $email ."', '". $message ."', '". $timestamp ."')");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 CCF-CSP 2023 第三题 解压缩(50%)
  • ¥30 comfyui openpose报错
  • ¥20 Wpf Datarid单元格闪烁效果的实现
  • ¥15 图像分割、图像边缘提取
  • ¥15 sqlserver执行存储过程报错
  • ¥100 nuxt、uniapp、ruoyi-vue 相关发布问题
  • ¥15 浮窗和全屏应用同时存在,全屏应用输入法无法弹出
  • ¥100 matlab2009 32位一直初始化
  • ¥15 Expected type 'str | PathLike[str]…… bytes' instead
  • ¥15 三极管电路求解,已知电阻电压和三级关放大倍数