Solved

Submit

Posted on 2002-04-02
6
154 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
ID: 6913036
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_
ID: 6913078
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
ID: 6913438
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 5

Expert Comment

by:andriv
ID: 6913449
Glad we can help
0
 
LVL 2

Expert Comment

by:Marsman_
ID: 6913905
It does!?
That's pretty weird!
0
 
LVL 5

Expert Comment

by:andriv
ID: 6913966
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

820 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