dqnk57224 2014-11-07 23:40
浏览 67
已采纳

PHP表单不保存信息到mySQL数据库错误

Ok, so I have tested if the database is connected and it is. But, my problem is that the form won't go onto the output.php page to be able to save it to the database, I'm getting a NOT FOUND error. I have included a screenshot of the error, the code seems right to me but I can't figure out why it can't find the page when it's clearly in the FOLDER. Thanks for your time in advance.


form.php file


<!DOCTYPE HTML> 
<html>
<head>
</head>
<body> 

<?php
$fname = $surname = "";
?>

<h2>Fill out user details</h2>
<form action="output.php" method="post" > 
   First Name: <input id="fname" type="text" name="fname" >
   <br><br>
   Surname <input id="surname" type="text" name="surname">
   <br><br>

   <input type="submit" value="Submit">
</form>

</body>
</html>

output.php file

<!DOCTYPE HTML> 
<html>
<head>
</head>
<body>
<?php $fname=$_POST["fname"]; 
$surname=$_POST["surname"];?>

<!--Connect to the database-->
<?php
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully"; ?>

<?php
$sql="INSERT INTO 'user'.'user_tbl'('user_fname','user_surname') VALUES ('$fname','$surname')";
mysqli($conn,$sql);
?>

<?php
header("Location: http://localhost/mysite/");
?>
</body>
</html>

Error

  • 写回答

1条回答 默认 最新

  • dongyi5070 2014-11-08 00:13
    关注

    There are a few things wrong with your code.

    You are using the wrong identifiers for your table and columns, being single quotes.

    Use bacticks

    $sql="INSERT INTO `user`.`user_tbl`(`user_fname`,`user_surname`) VALUES ('$fname','$surname')";
    

    Then there is this line mysqli($conn,$sql); it should be mysqli_query($conn,$sql);

    or

    if(!$result = $conn->query($sql)){
        die('There was an error running the query [' . $conn->error . ']');
    }
    

    Then this

    $conn = new mysqli($servername, $username, $password);
    

    there should be a 4th parameter for it being for the database

    $conn = new mysqli($servername, $username, $password, $database);
    

    however, I see no variables set for any of these.

    Here is an example taken from http://php.net/manual/en/function.mysqli-connect.php

    $link = mysqli_connect("myhost","myuser","mypassw","mybd") 
    or die("Error " . mysqli_error($link)); 
    

    or using your variables and the one that was missing, and replacing with your own credentials

    $servername = "localhost"; // or what your service provider said to use
    $username = "username"; // your username
    $password = "password"; // if one is needed. Leave blank if no password is needed
    $database = "your_database"; // your database name
    
    $conn = new mysqli($servername, $username, $password, $database);
    

    Plus, your present code is open to SQL injection. Use prepared statements, or PDO with prepared statements, they're much safer.

    Checking for errors would have signaled the error, if you had used mysqli_query instead of just mysqli

    or die(mysqli_error($conn)) to mysqli_query()


    However, you are doing what is called "outputting before header" with HTML above your PHP

    the header being

    header("Location: http://localhost/mysite/");
    

    you need to place your HTML under PHP.

    Actually, you don't need the following tags for your SQL/PHP

    <!DOCTYPE HTML> 
    <html>
    <head>
    </head>
    <body>
    

    and

    </body>
    </html>
    
    • Just the PHP/SQL.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试