Current location: Hot Scripts Forums » Programming Languages » PHP » PHP Experts! Need your help


PHP Experts! Need your help

Reply
  #1 (permalink)  
Old 06-27-10, 03:29 PM
wizowizo wizowizo is offline
New Member
 
Join Date: Jun 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
PHP Experts! Need your help

Hi,

I have designed a simple competition which contain 3 tables, one of these tables is for the questions..
in this code I have managed to retrieve question no. 1 from the database, and I can changed it through this form and press change to send the data to the table.
Everything works fine up to now.. but I want to make it works for all the questions in my table, so I can change them all in one page.. I think there should be a way to do it by loop rather than retyping the whole code again for each question.

At the same time it would be very helpful if I can make it both for questions and answers.. in other words I can change the question and the answer and press one button to update the database table..


Any help?
PHP Code:

PHP Code:

<?php 

require ("connect.php"); 
if (isset(
$_POST['submit']) && isset($_POST['tochange1'])) { 
   
$tochange1 $_POST['tochange1']; 
   
$changeQ1 mysql_query("UPDATE questions SET Question='" mysql_real_escape_string($tochange1) . "' WHERE QuestionNO = 1"); 

$resultQ1 mysql_query("SELECT question FROM questions WHERE QuestionNO=1"); 
while (
$row1 mysql_fetch_assoc($resultQ1)) { 
   echo 
$row1["question"]; 

echo 

<form method='POST'> 
<input type='text' name='tochange1' /> 
<input type='submit' name='submit' value='Change' /> 
</form> 
<br /> 
"

?>
Reply With Quote
  #2 (permalink)  
Old 06-27-10, 08:16 PM
Jcbones Jcbones is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
What is the database structure for all 3 of your tables?

Using just the data from your script, it should look something like:

PHP Code:

<?php 
require ("connect.php"); 
if (isset(
$_POST['submit'])) { 
   foreach(
$_POST['change'] as $key => $value) {
        
$id = (int)mysql_real_escape_string($key);
        
$question mysql_real_escape_string($value);
        
$sql "UPDATE questions SET Question = '$value' WHERE QuestionNO = $id";
        if(!
mysql_query($sql)) {
            echo 
'<br />There was a problem with: <br />' $sql '<br />' mysql_error();
        }
        else {
            echo 
'All questions Updated!';
        }
    }


echo 
"<form method='POST'> ";

$resultQ1 mysql_query("SELECT Question, QuestionNO FROM questions"); 
while (
$row1 mysql_fetch_assoc($resultQ1)) { 
   echo 
'<input type="text" name="change[' $row1['QuestionNO'] . ']" value="' $row1['Question'] . '" />';
}

echo 
'<input type="submit" name="submit" value="Change" /> 
</form> 
<br />'
;
?>
Reply With Quote
  #3 (permalink)  
Old 06-28-10, 07:44 AM
wizowizo wizowizo is offline
New Member
 
Join Date: Jun 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Many thanks for your reply.. attached is the Database Diagram.

I have tried the code you modified, but I got this error:

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\testo.php on line 20
Attached Images
File Type: jpg DBDiagram.jpg (38.1 KB, 122 views)
Reply With Quote
  #4 (permalink)  
Old 06-28-10, 02:25 PM
Golith Golith is offline
Newbie Coder
 
Join Date: Jun 2010
Posts: 87
Thanks: 6
Thanked 1 Time in 1 Post
i find a $con gets me out of it

Use this acute accent ( ` ), and character which goes down from the right to the left. It's on the left hand side of my keyboard, on the left of (1 !) and above ( TAB ).

So it will be like: resultQ1 = mysql_query("SELECT Question, QuestionNO FROM `questions`");
the other reasons maybe
1.no connection present which means null (bolean) is returned
2. no rows present in db. --> can u query the db with num_rows and get the value 1 to return?

other than that i'm fresh out

Last edited by Golith; 06-28-10 at 02:41 PM. Reason: more indepth
Reply With Quote
  #5 (permalink)  
Old 06-28-10, 04:11 PM
wizowizo wizowizo is offline
New Member
 
Join Date: Jun 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Hello,

I have tried each of these options, but still got the same error..

any help?
Reply With Quote
  #6 (permalink)  
Old 06-29-10, 04:18 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
replace this line
PHP Code:

$resultQ1 mysql_query("SELECT Question, QuestionNO FROM questions"); 

with this one
PHP Code:

$resultQ1 mysql_query("SELECT Question, QuestionNO FROM questions") or die(mysql_error()); 

and tell us what you see
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
2 profitable script sites for sale cms-master.com General Advertisements 3 07-03-07 10:17 AM
help with error messages.. please APuppyDog PHP 2 10-05-06 11:09 PM
PHP Not Working ProjectJustice PHP 2 06-25-06 07:37 PM
PHP Downside--Solutions? Amulet PHP 10 07-15-05 08:26 AM


All times are GMT -5. The time now is 08:15 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.