Solved

Submit

Posted on 2002-04-02
6
161 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 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
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

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…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

717 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