I have been struggling for a week as to why the database cannot insert a piece of data! ?
石头
石头 2018-02-27 23:07:57
0
7
2070

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);

?>


石头
石头

reply all(4)
ardunio

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.

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

Try it

猪哥
$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

菜鸟互啄

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 Thank you very much for your reply. Navicat is installed, I will try to correct other problems immediately.
    石头 author 2018-02-28 12:28:31
  • reply Are spaces allowed in PHP? ?
    石头 author 2018-02-28 12:34:05
  • reply 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.
    菜鸟互啄 author 2018-03-01 09:05:07
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template