Solved

Submit

Posted on 2002-04-02
6
143 Views
Last Modified: 2006-11-17
the following code doesn't work :'( plzzzzz help me with it..
---------------------------------------------------------
<?
 mysql_connect("localhost","","") or  die ("Unable to connect to database");
 mysql_select_db("test_toaster") or die ("Unable to select database test_toaster");

 if ($Private!="Y") { $Private="N";}

 $question_insert = mysql_query("INSERT INTO question VALUES ('','$Content','Fill in','','','$Private',4,$Difficulty_Level,1111,2222)");
 $id=mysql_insert_id ();

   if ($answer1!="")
   $answer1_insert=mysql_query("INSERT INTO question_correct_answers (question_id,correct_answer) VALUES ($id,'$answer1')");
   if ($answer2!="")
   $answer2_insert=mysql_query("INSERT INTO question_correct_answers (question_id,correct_answer) VALUES ($id,'$answer2')");
   if ($answer3!="")
   $answer3_insert=mysql_query("INSERT INTO question_correct_answers (question_id,correct_answer) VALUES ($id,'$answer3')");   $answer_insert=mysql_query("INSERT INTO question_correct_answers (question_id,correct_answer) VALUES ($id,'$answer1')");
   if ($answer4!="")
   $answer4_insert=mysql_query("INSERT INTO question_correct_answers (question_id,correct_answer) VALUES ($id,'$answer4')");

 if (($question_insert== TRUE) and (($answer1_insert==TRUE) || ($answer2_insert==TRUE) || ($answer3_insert==TRUE) ||($answer4_insert==TRUE))
 {
   print "Your question has be successfully stored<BR>";
 }
 mysql_close();
?>
0
Comment
Question by:almaha
  • 3
  • 2
6 Comments
 
LVL 5

Accepted Solution

by:
andriv earned 10 total points
Comment Utility
did you remove your username and password from the following line for security or there isn't any.

mysql_connect("localhost","","") or  die ("Unable to connect to database");

mysql_select_db("test_toaster",$dblink) or die ("Unable to select database test_toaster");

If it's because you don't have any set then use:

mysql_connect("localhost")

Also store the return link to a variable and use it within the other mysql queries:

$dblink = mysql_connect("localhost")

$question_insert = mysql_query("INSERT INTO question VALUES ('','$Content','Fill in','','','$Private',4,$Difficulty_Level,1111,2222),$dblink");

And do the same for the other queries.

If this is not the problem tell me what error message you are getting or what is not happening.



0
 
LVL 2

Expert Comment

by:Marsman_
Comment Utility
You really do not need to save the return thing from the database, php does this automatically when you only have one db-connection.

I see something very wrong in your script:

if (($question_insert== TRUE) and (($answer1_insert==TRUE) || ($answer2_insert==TRUE) || ($answer3_insert==TRUE)
||($answer4_insert==TRUE))

This of course should be:

if (($question_insert== TRUE) && (($answer1_insert==TRUE) || ($answer2_insert==TRUE) || ($answer3_insert==TRUE)
||($answer4_insert==TRUE))

Php doesn't recognize "and" instead use "&&"

 
0
 

Author Comment

by:almaha
Comment Utility
ThX andriv. the query syntax was part of the problem. the major problem was the the conditional statement with one and the 4 or's.
Marsman, PHP recognizes "and"! it works with me in another scripts..
Finally the code is working with me. here you go:
-------------------------------------------
<?
 mysql_connect("localhost","","") or  die ("Unable to connect to database");
 mysql_select_db("test_toaster") or die ("Unable to select database test_toaster");

 if ($Private!="Y") { $Private="N";}

 $question_insert = mysql_query("INSERT INTO question VALUES ('','$Content','Fill in','','','$Private',4,$Difficulty_Level,1111,2222)");
 $id=mysql_insert_id ();

   if ($answer1!="")
   $answer1_insert=mysql_query("INSERT INTO question_correct_answers (question_id,correct_answer) VALUES ($id,'$answer1')");
   if ($answer2!="")
   $answer2_insert=mysql_query("INSERT INTO question_correct_answers (question_id,correct_answer) VALUES ($id,'$answer2')");
   if ($answer3!="")
   $answer3_insert=mysql_query("INSERT INTO question_correct_answers (question_id,correct_answer) VALUES ($id,'$answer3')");
   if ($answer4!="")
   $answer4_insert=mysql_query("INSERT INTO question_correct_answers (question_id,correct_answer) VALUES ($id,'$answer4')");

 //if (($question_insert== TRUE) and ((((($answer1_insert==TRUE) || ($answer2_insert==TRUE)) || (($answer3_insert==TRUE))) ||($answer4_insert==TRUE))
 $first_two = ($answer1_insert==TRUE) || ($answer2_insert==TRUE);
 $second_two = ($answer3_insert==TRUE) || ($answer4_insert==TRUE);
 $answers_insert = ($first_two==TRUE) || ($second_two==TRUE);

 if  (($question_insert== TRUE) and  ($answers_insert==TRUE))
 {
   print "Your question has be successfully stored<BR>";
 }
 mysql_close();
?>
-------------------------------------------------------
ThX guys,
that was really helpful :)
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 5

Expert Comment

by:andriv
Comment Utility
Glad we can help
0
 
LVL 2

Expert Comment

by:Marsman_
Comment Utility
It does!?
That's pretty weird!
0
 
LVL 5

Expert Comment

by:andriv
Comment Utility
Marsman you can also use "or" instead of ||

most people do not use "and" or "or" because it makes it harder to spot an error.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now