Beginner's question: can the value in $ _ GET be read by mySQL in php?

Source: Internet
Author: User
{Code ...} in {code ...} the id_p value is always 0, and other values are normal. I don't know why. $ _ GET [post] has a value, which can be used at the top and bottom of the code, but cannot be used in this SQL statement... I tried a lot of methods, such as adding $ a $ _ GET [pos...
 

In

$sql = "insert into reply (id,id_p,time,content) values('','$_GET[post]',now(),'$_POST[content]')";

The id_p value is always 0, and other values are normal. I don't know why
. $ _ GET [post] has a value, which can be used at the top and bottom of the code, but cannot be used in this SQL statement...

I tried a lot of methods, such as adding $ a =$ _ GET [post] before the code, use $ row [id] (the id in the message table is actually the value of $ _ GET [post]). The value cannot be read;

Is it

Echo"
 
Question?

Reply content:
  

In

$sql = "insert into reply (id,id_p,time,content) values('','$_GET[post]',now(),'$_POST[content]')";

The id_p value is always 0, and other values are normal. I don't know why
. $ _ GET [post] has a value, which can be used at the top and bottom of the code, but cannot be used in this SQL statement...

I tried a lot of methods, such as adding $ a =$ _ GET [post] before the code, use $ row [id] (the id in the message table is actually the value of $ _ GET [post]). The value cannot be read;

Is it

Echo"
  
Question?

I think you 'd better define a variable to GET the value, because I have tried it before. it seems that writing $ _ GET in SQL statements is not feasible, personal ignorance.
PS: It seems that there are many low-level errors in the code... it is recommended that you change them to test...

Set$_GET[post]Change{$_GET['post']}

In addition, you have to strengthen the cultivation of coding habits. the indentions of this code make you crazy.Echo" The output html is useless.>Closed, and double quotation marks are not used for attributes... Need to cultivate the programmer's temperament

First, you need to understand that mysql will not execute any value submitted by any page;
Second, according to the code you post, php will parse the value submitted on your page and generate an SQL statement;
Again, your code is quite uncomfortable to write, unindent, and have no sense of attention;
Again, please confirm the submission method of your page, whether it is GET or POST. if you are not sure, please directly use REQUEST, GET, POST, and so on. this is really awkward;
Finally, please""When writing complex variables, remember to add {}, as shown in figure{$POST['post']}

Supplement:
Check whether the parameters submitted on your page arepost
You can print all submitted values on the page of this code, and add the following code at the beginning:
echo

;</code><br /><code>var_dump($_REQUEST);</code><br /><code>echo ;

Tinyint is used for your field, and tinyint is from-128 ~ Will it overflow if it is 127?
Also, in essence, int (10) and int (1) are no different, so it is generally not a true or false or status field, and int (1) is not used ), you can just give an int (10.

Run this command to change the table structure.

$sql = "alter table reply change id id int(10) NOT NULL AUTO_INCREMENT";mysql_query($sql) or die(mysql_error());$sql = "alter table reply change id_p id_p int(10) NOT NULL";mysql_query($sql) or die(mysql_error());

Then, change the code to this, and display all the information.

if($_POST['submit']){  $sql = "insert into reply (id_p,time,content) values('{$_GET['post']}',now(),'{$_POST['content']}')";  echo $sql;  mysql_query($sql) or die(mysql_error());  $sql = "select * from reply order by id DESC limit 1";  $query = mysql_query($sql);  $result = mysql_fetch_array($query);  var_dump($result);//  echo "
    ";}

There are several SQL problems:

  • Since id is an auto-incrementing primary key, you do not need to explicitly insert it in SQL.
  • It is best to enclose variables in double quotation marks {}.
  • Use mysql_query to handle errors, such as or die (mysql_error ().

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.