473,387 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

For-loop and mysql

Well, I'm almost completely new to php, but have a project, where I'm adding articles to a sql database. The articles come from a html form, and are added to the db with php. Wasn't sure which forum I should go to.

In my code, if get a list of subjects separated by ";", I then make that string into an array with explode, and try to add a row to the database for every subject. But i get "Unable to insert subjects to database" every time i run it.

Expand|Select|Wrap|Line Numbers
  1. //Explode the subjects string into seperate subjects
  2. $subjects  = $_POST["article_subjects  "];
  3. $subjects_arr = explode(';', $subjects, 10);
  4.  
  5. for ($i = 0; $i <= 10; $i++) {
  6.     if ($subjects_arr[$i] != null){
  7.         $add_subject_sql = "INSERT INTO artikel_subject (artikel_id, artikel_subject) VALUES ('".$article_id."','".$subjects_arr[$i]."')";
  8. $add_subject_res = mysql_query($add_subject_sql) or die("Unable to insert subjects to database");
  9.     }
  10. }
  11.  
It works if I drop the loop and don't use $subjects_arr[$i], but use something like
Expand|Select|Wrap|Line Numbers
  1. $add_subject_sql = "INSERT INTO artikel_subject (artikel_id, artikel_subject) VALUES ('".$article_id."','".$_POST["article_subject"]."')"; 
But I wanted to add several keywords, and put them into different rows. Did i make an error, or is there a better way to do this?
Nov 4 '07 #1
2 1215
pbmods
5,821 Expert 4TB
Heya, Dorland. Welcome to TSDN!

Try this instead:

Expand|Select|Wrap|Line Numbers
  1. $add_subject_res = mysql_query($add_subject_sql) or die("Unable to insert subjects to database:  " . mysql_error());
  2.  
Nov 5 '07 #2
Thanks :). Have not used the error function before. It turned out that it was an error in the db afterall. It was the wrong key which was primary.
Nov 7 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Xerxes | last post by:
Hi, is there any script to authenticate an email address entered in a form field? I used the php mail() function, using the following (where my email field on the form is called "email"): ...
4
by: Craig Bailey | last post by:
Anyone recommend a good script editor for Mac OS X? Just finished a 4-day PHP class in front of a Windows machine, and liked the editor we used. Don't recall the name, but it gave line numbers as...
0
by: Verizon | last post by:
Has anybody ever heard of support for the book: "Secure PHP Development" by: Mohammed J. Kabir I'm trying to run one of his PHP solutions called "Web Forms Manager" I haven't been able to...
0
by: Robert Freund | last post by:
I've been looking for a good and free code beautifier that runs on linux. About the only ones I found where Trita (www.trita.com), which costs money and only runs on windows. The other one was...
33
by: Frank | last post by:
What is the best IDE for developing PHP applications? What do you use and why? Thanks.
4
by: john Q | last post by:
I found a PHP based email obfuscator for use in websites, where the source HTML (PHP, actually) file doesn't contain the actual email address in the Mailto tag, but generates it and that...
3
by: MT | last post by:
Hello Everyone: I am running XP Pro with IIS 5.1. Which is the best PHP version to install: CGI, CLI or ISAPI?? The installation manual lists all three but does not say which is most...
5
by: ChronicFatigue | last post by:
Hello My current host has register_globals switched on in their php.ini file. Would it be prudent for me to design code which works when register_globals is switched off in case I switch hosts...
1
by: Mark Hanford | last post by:
I'm after a regular expression that looks through a string for a certain code, and then replaces part of that code with some markup: So look in this string: this text contains (T123) a code. ...
0
by: Sugapablo | last post by:
I'm wondering if anyone knows of any code out there that someone might be sharing to generate the printing dimensions required for printing bank checks or tax forms such as 1099s? What I'd like...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.