简体   繁体   中英

Using a PHP/HTML form to insert information into 2 MySQL tables

Using a form I am inserting the information from 3 text fields and 2 checkboxes into a MySQL database.

The database consists of 2 tables with the following rows:

id - articletitle - articleorganization - articledate - articleurl

id - article_tags - articlid - tagid 

The checkboxes use an array and are coded thus:

<input type="checkbox" name="articletags[]" value="geology" id="articletags_0" />
<input type="checkbox" name="articletags[]" value="astronomy" id="articletags_1" />

And the MySQL statement looks like this:

mysql_query("INSERT INTO articles SET articletitle='$articletitle',
 articleorganization='$articleorganization',
 articledate='$articledate',
 articleurl='$articleurl' ")

mysql_query2("INSERT INTO articles_tags SET articletags='$articletags' ")

However, when I visit the page in a web browser it shows a blank page.

In case anyone is wondering, the reason I'm using checkboxes is because the articles will need to be tagged with a combination of tags, and this will ensure that consistency is used in the tagging. Also, I have a form used to edit the articles, and the database structure could, I believe, be used to make the checkboxes "checked" on the edit page.

Any advice is greatly appreciated.

what is mysql_query2 ? are you sure you put semicolon after query ?

mysql_query("INSERT INTO articles_tags SET articletags='$articletags' ");

Change mysql_query2('*******) to mysql_query() and enable your error reporting to check error comes in the code. Echo your query and run directly in database to check any SQL related error.

The blank page is actually from a syntax error. To see syntax errors, edit the php.ini or add this line to the top of your code error_reporting(E_ALL);

Change mysql_query2() to mysql_query()

Your SQL sytax is incorrect...

If the data is already in the database you want to use UPDATE.

mysql_query("UPDATE articles SET articletitle='$articletitle',
 articleorganization='$articleorganization',
 articledate='$articledate',
 articleurl='$articleurl' ");

mysql_query("UPDATE articles_tags SET articletags='$articletags' ")

This is how you insert data:

mysql_query("INSERT INTO articles (articletitle,
 articleorganization,
 articledate,
 articleurl) VALUES ($articletitle,$articleorganization,$articledate,$articleurl) ");

Note : You should use mysql_error() to check for syntax errors.

mysql_query("INSERT INTO articles_tags SET articletags='$articletags' ") OR DIE(mysql_error());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM