?
Solved

Submit

Posted on 2002-04-02
6
Medium Priority
?
163 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 5

Accepted Solution

by:
andriv earned 40 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
Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

 
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

771 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