473,396 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Updating mysql table

14
I want to use a query that will update a table if a specific entry exists, or inserts the data if it doesn't. The code below shows what i have so far (taken from a previous post and modified)

[PHP]
$result = mysql_query("UPDATE answersmod".$ModNum." SET answer='$A1' WHERE user_id='$lg_name' AND Q_Num = '$Q_Num'");
if (mysql_affected_rows($result) == 0)
{
// No records updated, so add it
$result = mysql_query("INSERT INTO answersmod".$ModNum." (User_ID, Q_Num, Answer) VALUES ('$lg_name', '$Q_Num', '$A1')");
if (mysql_affected_rows($result) == 0)
{
echo "No insert or update performed";
} else { echo "All done";}// end if
}//end if
[/PHP]

It does not update or insert. instead i get the following:
Expand|Select|Wrap|Line Numbers
  1. Warning: mysql_affected_rows(): supplied argument is not a valid MySQL-Link resource in C:\Program Files\xampp\htdocs\Lilly\PageFiles\ModDisplayQuestions.php on line 62
  2. Warning: mysql_affected_rows(): supplied argument is not a valid MySQL-Link resource in C:\Program Files\xampp\htdocs\Lilly\PageFiles\ModDisplayQuestions.php on line 67
  3. No insert or update performed
  4.  
previous posts have said that this is often caused by an incorrect connection, but my connection to the db works fine on all other pages (it is in a seperate module so doesn't change).

any ideas? I have to have this completed by tuesday afternoon so a speedy reply would be much appreciated!

thanks in advance for any help
Apr 23 '07 #1
1 1611
Franky
14
I have changed the code slightly and have a different issue:

[PHP]
function displayQuestions($ModNum){
global $result, $query, $a;

//If submit is not set do this, else do next (process the form)
if (!isset($_POST['Submit']))
{
dbConnect();

$query = "SELECT * FROM questionsmod".$ModNum." ORDER BY RAND() LIMIT 5";
$result = mysql_query($query); ?>

<p>Below is a set of questions reviewing the content covered in this module. Answer all questions and click the 'Submit' button at the bottom of the page. You will then get your results.</p>
<TABLE>
<FORM method="post" name="Answers" action="<?php echo $PHP_SELF; ?>">

<?php
$a=1;
while ($row = mysql_fetch_assoc($result))
{ ?>
<TABLE>
<TR><TD><b><? echo "$a. ", $row ['Question']. "<br/>" ?></b></TD></TR>
<TR><TD><input type="radio" name="<?php print 'Question'.$a ?>" value="1"> <? print $row ['Opt_1']. "<br/>" ?></TD></TR>
<TR><TD><input type="radio" name="<?php print 'Question'.$a ?>" value="2"> <? print $row ['Opt_2']. "<br/>" ?></TD></TR>
<TR><TD><input type="radio" name="<?php print 'Question'.$a ?>" value="3"> <? print $row ['Opt_3']. "<br/>" ?></TD></TR>
<TR><TD><input type="radio" name="<?php print 'Question'.$a ?>" value="4"> <? print $row ['Opt_4']. "<br/>" ?></TD></TR>
</TABLE>
<?php
print "<br/>";
$a++;
}//end while
?>

<TR><TD><input type="submit" name="Submit" value="Submit"></TD></TR>
</FORM>
</TABLE>
<?php
} else {
//Call to ModDBQueries to connect to the database
dbConnect();

$lg_name = $_SESSION['author'];

$i=1;
while ($i <= 5)
{
$Answer = $_POST['Question'.$i];
echo "Answer ".$i." = ".$Answer."<br>";

//Inserting answer into database
$result = mysql_query("INSERT INTO answersmod".$ModNum." (User_ID, Q_Num, Answer) VALUES ('$lg_name', 'Question', '$Answer')");
$i++;
}
}//end if
}//end function "displayQuestions"
[/PHP]


The first section shows the 5 questions and options selected from the db. The SELECT * part gets this info, including the question number of the randomly selected questions.

The second part displays the users answer for each question and (in theory) updates the relevant database table with their answer. However, to do this successfully, as i loop through their answers i need the question number (i.e. the one from the questions table in the db, not the one on the page, i.e. 1,2,3,4 or 5).

Any way i can get this from the array output going on the in the loop in the first section? Can i append a variable name with the incrementing $a and store it in that (i.e. just above - print "<br/>"; $a++; - have $Num,$a = $row['Q_Num']; where $Num,$a would be Num1, Num2, etc)?

Or is there a better way to display and process the form (including updating the db)?
Apr 23 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: | last post by:
Hello, Sorry to ask what is probably a simple answer, but I am having problems updating a table/database from a PHP/ PHTML file. I can Read From the Table, I can Insert into Table/Database, But...
11
by: Jason | last post by:
Let's say I have an html form with 20 or 30 fields in it. The form submits the fields via POST to a php page which updates a table in a database with the $_POST vars. Which makes more sense? ...
0
by: Knepley, Jim | last post by:
I'm basing some work on Joe Celko's excellent idea of using nested sets to represent an organizational structure as opposed to an adjacency list. By and large it's a great idea, but not without its...
0
by: Jeremiah Jacks | last post by:
I just upgraded to MySQL 4.0.14-standard for RedHat Linux and am using = the pre-compiled binaries. I have a database with INNODB tables. When I insert a row into one of the child tables, I get...
0
by: Dan Jones | last post by:
I have a database of books that was originally created as a flat file. Each record has a number of fields, including the authors name. I'm trying to convert the database to something a little more...
1
by: Brett Magill | last post by:
Hello all, Looking for some help here. BTW, to e-mail me directly, should you prefer, take the *nomail*. out the reply address. I uploaded data from another program ( GNU R-2.2.0 @...
33
by: bill | last post by:
In an application I am writing the user can define a series of steps to be followed. I save them in a sql database using the field "order" (a smallint) as the primary key. (there are in the range...
10
by: help4me | last post by:
I am having trouble updating a table. The logic seems so simple but I just can’t get it to work. The table is named test. The column names are passcode, name, address, city, state, zip and email....
2
by: julie18881 | last post by:
I may be being really stupid here, i have spent the last 3 hours looking round your site and some other for answers to my problem, but have not had much luck (possibly cuase my brain just isn't...
9
by: Steve67 | last post by:
Need a little assistance with a VBA code for updating tables through a form. I have a form with two text boxes and a command button. One text box is named txtNewPartNumber and the other text box is...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.