About the PHP update database

Source: Internet
Author: User
Tags php database php file editor
This post was last edited by Ur_not_alone on 2013-06-03 11:55:36

Just started to learn PHP encountered some basic problems please help us
An example from the Internet, do the next exercise.
When I fill out the page content click Submit, the page opens insert.php by default instead of executing the code inserted in the insert.php database.
The code is as follows
HTML page
 
  Untitled Document


insert.php
 
  0) echo "xinwentianjiachenggong! ";? >
 
  Fanhuijixutianjiaxinwen


connect_mysql.php
 
  


In addition, if I want to achieve the ability to upload pictures of how to do ah, to provide ideas, if someone has an example of the best, thank you


Reply to discussion (solution)

Include ("connect_mysql.php");
Mysql_open ();

Include ("connect_mysql.php");
Mysql_open ();
No Ah, or click Submit, then open insert.php, no insert operation ...

What do you mean by opening insert.php?

Landlord to confirm the PHP environment is not a problem???

did not see the landlord's Submit button has an OnClick event ah?

Hey look at so many people's code, or LZ's best understanding!!!!

What do you mean by opening insert.php?
Open the meaning is, will use the system default PHP file editor to open insert.php, for example, I this default use EmEditor open ...

PHP environment Well, no, the file is not placed in the Web server root directory, how to access it?

PHP environment Well, no, the file is not placed in the Web server root directory, how to access it?
PHP environment configuration should be no problem, I use eclipse_php, files placed in D:\Program Files\apache software foundation\apache2.2\htdocs\ Testinsert (Testinsert is the project name), then access http://localhost/TestInsert/Page.html fill in the data, click Submit, pop up a Http://localhost/TestInsert /insert.php page, but nothing has changed in the database.


What do you mean by opening insert.php?
Open the meaning is, will use the system default PHP file editor to open insert.php, for example, I this default use EmEditor open ...
This is wrong, ignore it, should be pop up a http://localhost/TestInsert/insert.php page, ie open the

The download page shows that the PHP environment has not been matched successfully

The download page shows that the PHP environment has not been matched successfully
I think it is not the environment, if I directly write the INSERT statement in a PHP (the value is directly written), you can insert data into the database, but this is not available from the page.

The download page shows that the PHP environment has not been matched successfully
And I checked, Php5,mysql,apache and the installation of Eclipse, didn't see any problem ...

, the problem appears on the linked database,
Link database function has a problem, comment out the function section below can be executed directly!
function Mysql_open ()
//{
$mysql _servername= "localhost"; //
$mysql _username= "root";
$mysql _password= "11111";
$mysql _dbname= "new";

$conn =mysql_connect ($mysql _servername, $mysql _username, $mysql _password);
mysql_query ("Set names UTF8"); UTF8
mysql_select_db ($mysql _dbname, $conn);
return $conn;
//}

, the problem appears on the linked database,
Link database function has a problem, comment out the function section below can be executed directly!
function Mysql_open ()
//{
$mysql _servername= "localhost"; //
$mysql _username= "root";
$mysql _password= "11111";
$mysql _dbname= "new";

$conn =mysql_connect ($mysql _servername, $mysql _username, $mysql _password);
mysql_query ("Set names UTF8"); UTF8
mysql_select_db ($mysql _dbname, $conn);
return $conn;
//}
This is the same method as the 1 floor, not the problem.

 0) echo "xinwentianjiachenggong! ";? >
 
  
 Fanhuijixutianjiaxinwen


I changed the code to look like this through the error message is found because the ID field is not filled to cause the insertion to fail, the above comment part of the problem. I hope that we can help more, if you feel that the score is not enough for me to add, just feel that the post trouble.

$result = mysql_query ($sql 1);
The resulting result is not a value such as 1, 2, 3 stored in the database, and then causes the update to fail, prompting Error:incorrect integer value: ' for column ' id ' at row 1, want to ask how you can get the correct ID by the query, or the result value and then insert the database.

If the ID is a primary key, then because the primary key cannot be duplicated, it is obviously wrong for you to assign Max (ID) to it! should be max (ID) + 1
If the ID is a primary key and is self-increasing, you do not need to assign a value to the ID
If the ID is not a primary key, but is not allowed to be empty, then it is wrong to assign a value to the ID.

In fact, just print out the results of mysql_error (), it's all clear.

$id = MySQL I_fetch_assoc ($result); Connect with MySQL, here again with mysqli, must be wrong, and return an array
At least this is necessary.

$sql = "INSERT into new (id,title, content, time, news_cate_id)
VALUES
(' ". $id [' Max (ID) ']." ', ' ". $_post[" title "]." ', ' ". $_post[" Content "]." ', Now (), ". $_post[" c_id "].") ";

If your ID is a primary key, inserting the same ID is an error.

If the ID is the primary key, then because the primary key cannot be duplicated, it is obviously wrong for you to assign Max (ID) to it! should be max (ID) + 1
If the ID is a primary key and is self-increasing, you do not need to assign a value to the ID
if the ID is not a primary key, but is not allowed to be empty, then not assigning a value to the ID is the wrong

in fact, just print out the mysql_error () And everything is clear.
If the ID is a primary key, then because the primary key cannot be duplicated, it is obviously wrong for you to assign Max (ID) to it! should be max (ID) + 1 This I know, but it's written $id++; The
ID is the primary key, how to set the primary key self-increment ah. Is it possible to set the table when it is being built?

My database has the following SQL, what's wrong?

/*navicat MySQL Data transfersource server         : Testsource server Version:50153source Host           : Localhost:3306source D Atabase       : Newtarget server Type    : Mysqltarget server version:50153file Encoding         : 65001date:2013-06-04 14:2 1:53*/set foreign_key_checks=0;--------------------------------Table structure for ' new '-------------------------- ----DROP TABLE IF EXISTS ' new '; CREATE TABLE ' new ' (  ' id ' int (one) not null,  ' title ' varchar () default NULL,  ' content ' varchar () default N ULL,  ' time ' datetime default NULL,  ' news_cate_id ' int (one) default null,  PRIMARY KEY (' id ')) engine=innodb DEFAULT Charset=utf8;--------------------------------Records of new------------------------------INSERT into ' new ' VALUES (' 1 ', ' 111 ', ' 111 ', ' 2013-06-04 09:40:29 ', ' 1 '); INSERT into ' new ' VALUES (' 2 ', ' eee ', ' eeee ', ' 2013-06-04 13:28:20 ') , ' 1 ');

' id ' int (one) unsigned not NULL auto_increment,
So the primary key is self-increasing, right, but the corresponding
$sql = "INSERT into new (id,title, content, time, news_cate_id)
VALUES
(' ". $id." ', ' ". $_post[" title "]." ', ' ". $_post[" Content "]." ', Now (), ". $_post[" c_id "].") ";
How is this part supposed to change?

$sql = "INSERT into new (title, content, time, news_cate_id)
VALUES
(' ". $_post[" title "]." ', ' ". $_post[" Content "]." ', Now (), ". $_post[" c_id "].") ";

Know, only need to change to the original appearance on it, thank you for the knot, the knot paste before the first point, to express appreciation

  • Related Article

    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.