search

Home  >  Q&A  >  body text

I have been struggling for a week as to why the database cannot insert a piece of data! ?

The code is as follows, why the form data cannot be inserted into the database table. What's going on? !

<?php

$conn = mysqli_connect('localhost','root','','thdb');

$sql = "insert into info2 (name,tel,demo) values('$_POST[name]','$_POST[tel]','$_POST[demo]')" ;//Prepare sql

mysqli_query( $conn,$sql);

?>


石头石头2451 days ago2040

reply all(7)I'll reply

  • ******

    ******2018-02-28 23:22:13

    To troubleshoot the problem, you can start with the database. Write a SQL statement to insert a piece of data. If it works, check your PHP code again.

    reply
    0
  • 雕花笼

    雕花笼2018-02-28 17:24:36

    $sql = "insert into info2 (name,tel,demo) values ('{$_POST['name']}','{$_POST['tel']}','{$_POST['demo']}')" ;//准备sql

    Try it

    reply
    0
  • PHP中文网

    PHP中文网2018-02-28 15:13:11

    $sql = "insert into info2(name,tel,demo) values('$_POST[name]','$_POST[tel]','$_POST[demo]')" ;

    There is a problem with this statement. The difference between single quotation marks and double quotation marks is not clear. Single quotes do not interpret variables. Change to double quotes "$_POST[tel]"

    Another problem: After connecting to the database, the data table is not selected. Take a look and see what you are missing

    reply
    0
  • 菜鸟互啄

    菜鸟互啄2018-02-28 09:16:24

    I won’t tell you directly what’s wrong.

    First: You need to find errors yourself. Have you installed the navicat software? Print out the sql statement, copy it and run it, and check the running results. Other sql management software is also available.

    Second: I don’t know if it’s a problem you copied, or if it’s always like this. Space Space Space Pay attention to the spaces.

    Third: The first thing after connecting to the database is to print the database $con to determine whether it is connected.

    reply
    0
  • 石头

    Thank you very much for your reply. Navicat is installed, I will try to correct other problems immediately.

    石头 · 2018-02-28 12:28:31
    石头

    Are spaces allowed in PHP? ?

    石头 · 2018-02-28 12:34:05
    菜鸟互啄

    What I'm saying is, you have to be careful about leaving spaces. Many errors arise from the lack of spaces between the two. From a reading point of view, the code is so dense that no one wants to look at it.

    菜鸟互啄 · 2018-03-01 09:05:07
  • Cancelreply