Need Help In the PHP script and Mysql - php

Please help me with this:
I am creating a survey facility for the administrator. The administrator is asked for the number of questions in the survey. And based on this a row is created in the table containing survey info. Based on number of questions the same number of rows are created in the question table. This has 4 columns for the answers with each question id the corresponding number of rows are created for the same in the question table.
Now I am trying to give a form on UI controlled by a loop so that admin can enter questions and answers one by one and the question table is updated each time.
survey table (sur_id [auto increment column],sur_subject,Sur_frm_dt,sur_to_dt,sur_is_active)
question table ( sur_id, q_id [auto increment column] , q_txt,ans1,ans2,ans3,ans4)
The page asks for survey details: survey subject , dates, and no of ques
and a row is generated in survey table.The survey id created and number of question is passed to the create question page. $sid is survey id and $noq is number of questions.
The code is as follows. Please dont mind the novice logic and script:
$sid = intval ($_GET['ids']);
$noq = intval ($_GET['qn']);
for($noq !=0;$noq >=1;$noq--)
{
$q = "insert into sur_ques (sur_id) values ('$sid')";
$ex = mysql_query($q);
$rs = mysql_affected_rows();
if($rs ==1)
{
echo" Questions Rows Created Corresponding to Survey Subject";
}
?>
<form name="form1" method="post" action="<?php echo($PHP_SELF); ?>">
<table width="500" border="0" cellspacing="1" cellpadding="0">
<tr><br><b>Ques No-<?php echo"$noq";?></b></tr><br>
<tr><textarea name = "q" rows ="10" cols = "70" wrap = "hard" ></textarea></tr><br>
<tr><td><b>Ans 1:</b></td><td><input type="text" name="a1" size="37" /></td></tr>
<tr><td><b>Ans 2:</b></td><td><input type="text" name="a2" size="37" /></td></tr>
<tr><td><b>Ans 3:</b></td><td><input type="text" name="a3" size="37" /></td></tr>
<tr><td><b>Ans 4:</b></td><td><input type="text" name="a4" size="37" /></td></tr>
</table>
<input type = "submit" name="qa" Value = "Add Q&A" />
<input type ="reset" Value="Reset" />
</form>
<?
if ($_POST['qa'])
{
$id = mysql_insert_id();
$result = mysql_query("update ques set q_txt = '$q', ans1 = '$a1' ans2 = '$a2' ans4 = '$a4' ans4 = '$a4' where q_id = '$id'");
if($r = mysql_num_rows($result))
{
echo" Question and answers updated";
}
}
else
{
break;
}
}
?>

1 immediate problem I am able to see in your code is that you are using a comparison operator in your for loop where it should be an assignment operator $noq != 0 should be $noq = 0 or some other value – Trevor 0 secs ago

I think you are mixing up server side and client side. The code you posted is processed at the moment someone opens the page.
If you want the individual questions to be updated the moment the visitor presses the submit button, you will need to use ajax to send the information of that specific form to the server to be processed while the form remains the same / does not reload.
As it is now, every time you open your page, new rows are added based on the GET variables but no row is ever updated because you are never posting, I don´t see $PHP_SELF defined anywhere but I suppose you mean / want $_SERVER['PHP_SELF'].

Related

Writing single page logging tool in php

Im making a small php webpage which I plan to use to track on which subjects a helpdesk receives calls. My database has 3 important fields: id, name, and amount for each subject.
On my page I have a form with a dropdown list where you select a type of call and click submit. The idea is that every time you click submit the page reloads and the amount in the database for the chosen id is heightened by 1.
The form gives me the id and name for each call:
<form method="post" action="index.php">
<select class="select" id="calltype" name="calltype">
<?php
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<option value=".$row["ID"].">".$row["NAAM"]."</option>".PHP_EOL;
}
}
?>
</select></br>
<input class="input" type="submit" name="Submit" value="Submit">
</form>
This part works, if I echo $_POST['calltype'] I get the correct ID. What I can't get to work is the update statement which I want to heighten the counter, like:
if(isset($_POST['calltype']{
mysqli_query("UPDATE calls SET amount=(amount+1), WHERE id = $_POST['calltype']");
}
How would I go about this? I tried several methods but can't get it to work
besides for the extra comma, interpolation with the POST array like this is risky. maybe try:
mysqli_query("UPDATE calls SET amount=(amount+1) WHERE id = " . mysqli_real_escape_string($link, $_POST['calltype']) . " ;");

PHP MySQL maths update value in database [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
So ive got a rating system in place on a website im making. Currently each itema has a plus and minus button on either side as well as a visible rating level in this case its 3.1 for one of the items.
Im trying to programme both the plus and minus buttons in such a way that when the user presses them then the rating either increases or decreases by one.
Currently ive only implemented the minus button, however, when I click it the rating value doesnt decrease. It stays at 3.1. The page works fine and when i click on the "minus" button i get the error Call to a member function execute() on a non-object on line 67 which is the $stmt->execute()
Here is the code i have so far:
<?php
$results = $mysqli->query("SELECT * FROM programmes ORDER BY ProgrammeName ASC");
if ($results) {
$i=0;
echo '<table><tr>';
echo '<br/>';
echo '<br/>';
while($obj = $results->fetch_object())
{
echo '<td>';
echo '<div class="tvProgs">';
echo '<form method="post" id = "programmes" action="">';
echo "<input type=\"hidden\" name=\"progID\" value=\"".htmlentities($obj->ProgrammeID)."\" />";
echo '<div class="progImage"><img src="images/'.$obj->Image.'"></div>';
echo '<div class="progTitle"><h3>'.$obj->ProgrammeName.'</h3>';
echo '<div class="progRating"><h5>'.$obj->Rating.'</h5></div>';
echo '<div id = "btnMin"><input type="button" id ="minus" value ="-"/></div>';
echo '<div id = "btnPl"><input type="button" id ="plus" value ="+"/></div> ';
echo '<br/>';
echo '</form>';
echo '</div>';
echo '</td>';
$i++;
if ($i == 5) {
echo '</tr><tr>';
}
}
echo '</tr></table>';
}
if(isset($_POST['minus'])){
$newRating = 1;
$ID = $_POST['progID'];
$upsql = "UPDATE programmes SET Rating = Rating - $newRating WHERE progID='$ID'";
$stmt = $mysqli->prepare($upsql);
$stmt->execute();
}
?>
Ive kept the ID field hidden as i dont want that to be displayed on the web page. I just want to update the rating of a particular ID.
any informatino will be appreciated
Change your query from:
$upsql = "UPDATE programmes SET Rating = Rating - $newRating WHERE progID='$ID'";
To this:
$upsql = "UPDATE programmes SET Rating = Rating - $newRating WHERE ProgrammeID='$ID'";
Hope it should work..
For using _POST['some_name'] field on your form should have attribute name with value some_name
<input type="button" name="some_name" value="Some value"/>
Currently you have id="minus" which is not passed to a server. At least you should have:
<input type="button" id ="minus" name="minus" value="-" />
^ we got name here
And second thought - as your plus/minus buttons have no type="submit" I suppose your form doesn't submit, unless you have some hidden javascript.
Try setting you plus/minus buttons like:
<input type="submit" name="minus" value ="-"/>
<input type="submit" name="plus" value ="+"/>
Alternatively, you could use HTML5 number type input for your rating. Depending on the broswer you will have a little spinner that will do this task. Note that not all the broswers render this the same.
echo '<div class="progRating">
<h5><input type="number" id="rating" name="rating" value="'.$obj->Rating.'"/></h5></div>';
Then you have to update only the rating with its new value.
More info here: http://www.html5tutorial.info/html5-number.php

How to store the result of a radio button with a database value, back into the database?

Been stuck on this bit of code for a while now. What I'm doing in 'do_assignments.php' (below) is creating a loop that outputs each question in my database and every possible answer associated with that question.
<form method="post" action="user_storeresults.php"> $query = "SELECT * FROM questions, question_choices WHERE q_topic = 'PhoneGap' AND visible = 1 AND q_type = 'Multiple' AND questions.q_id = question_choices.question_id";
$result=mysql_query($query);
while($row = mysql_fetch_array($result)){
if($row['q_id'] != #$qid){
echo "<h4><strong>" . $row['q_string'] . "</strong></h4>";
#$qid = $row['q_id'];
}
echo "<input name='".$row['choice_id']."' value='".$row['correct_choice']."' type='radio' />";
echo $row['choice_string'] . "<br/>";
}
?>
<p><input type="submit" name="submit" value="Submit"><p>
</form>
<?php
The value of each radio button contains data from a field in 'correct_choice' in my database, where there is wrong answers (0) and a correct answer (1) associated with each question.
All of this works well, but because i'm trying to submit a post from a radio button that doesn't have a specific name or value (only what it retrieves from the database), i can't quite figure out how i would be able to retrieve these values in the next page 'user_storeresults.php'...
Any help would be much appreciated. If i'm being a bit too vague then say, and ill try and clear it up as much as possible.

how do you store multiple rows and column array in mysql

I have an array of checkboxes.
<input type="checkbox" name="selection[]" value="move" />
<input type="checkbox" name="selection[]" value="move2" />
<input type="checkbox" name="selection[]" value="move3" />
<input type="checkbox" name="selection[]" value="move4" />
Depending on the number of checkboxes selected, a table with corresponding number of rows is generated.
for($x=0; $x<$N; $x++)
{
echo nl2br("<td><textarea name=art[] rows=10 cols=30></textarea> </td><td><textarea name=science[] rows=10 cols=30></textarea></td></textarea></td><td><textarea name=method[] rows=10 cols=30></textarea></td><td><textarea name=criteria[] rows=10 cols=30></textarea></td></tr>");
}
I cannot tell how many table rows with corresponding columns will be generated each time. So how to write the code to insert each set of row array is a problem. I have tried the
$optionsVal = implode(",", $data);
but that only works to store the selected options and not for the generated table rows and columns.Please can anyone help with this. Thanks in advance
Okay so I think I understand a little better, but perhaps you should relay your question in other terms.
Basically my understanding is that you are accepting an uncertain (within the boundaries of the number of checkboxes you have) number of checkboxes, which there in turn generate a row for each selected check box.
If you want to store these generated rows in mySQL you need to post the data back to the database
$result = mysqli_query($query, $conn);
$row = mysqli_fetch_array($result);
You need to set a $result similar to this, and store your check box values in it
In this example if the end-user hits the save button it inserts the values from the check box into a variable
if(isset($_POST["savebtn"]))
{
//inserting the new information
$id = $_POST[""];
$name = $_POST[""];
//iterate through each checkbox selected
foreach($_POST["checkbox"] as $loc_id)
{
$query = "INSERT INTO table(ID, Loc_Code) VALUES('$id', '$loc_id')";
$result = mysqli_query($query, $conn);
}
?>
This was just kinda taken from another example, but you are way off with the implode, you need to save the results of the php selection to variables first, and then assign them rows in mySQL by looping through the selection
UPDATE:
Okay, so you got them in an array, seelction[] - this is good now you would want to check to see if a certain value is selected...
if (in_array("move2", $_POST['selection'])) { /* move2 was selected */}
then you want to put that into a single string - you were right with the implode method
echo implode("\n", $_POST['selection']);
then echo it out with a foreach loop
foreach ($_POST['selection'] as $selection) {
echo "You selected: $selection <br>";
}

Using input checkboxes with a database

So I'm trying to help a friend out by writing his guild an attendance tracker for raiding and whatnot. Right now my concept is to do a select * from the user column, and make a checkbox for each user, assuming that person showed up to raid, it would pass a "1" through the form, and their raid attendance would be incremented by 1. On the users page the overall attendance would be calculated as (raidAtt / raidsTotal)*100 (since joining).
My issue right now is that I don't really know how to get all this information passed using a single loop...
Right now my code is something like this:
<form action="raidattend.php" method="post">
<?php
mysql_connect("$database",$username,$password);
#mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM attend WHERE UserName = $v_member ORDER BY date desc";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
?>
<table>
<tr>
<th>Member</th>
<th>Attended?</th>
</tr>
<?php
$i=0;
while ($i < $num) {
$f1=mysql_result($result,$i,"UserName");
}
<tr>
<td><?php echo $f1; ?></td><td input type="checkbox" checked value="1">
And that's where I ran into issues. I'm not sure how to pass each user and the result of the checkbox back to the database. Once I understand how to do that it's just as simple as incrementing, but I'm pretty lost.
Thanks for any help!
Edit: To clarify, what I'm unsure of is how to break it up so each member gets updated, I understand that I need to use a submit and all that.
Edit 2: Stray }
You should change your checkbox so that they all have the same name (ie name="member[]"). This way, when you submit your form, all of the checked members will be in $_POST['member']. Then, just loop through $_POST['member'] and update your table.
<td><?php echo $f1; ?><td> <input type="checkbox" name="member[]" value=<?php echo "'$f1'"; ?> /></td>
This should give you the list of checkboxes with the names of the members that attended.
Here is a quick overview of how to do the update:
1.Loop through $_POST['member'] and increment the amount that person has attended :
foreach($_POST['member'] as $member)
{
mysql_query("update table_name set attended=(attended+1) where username='$member'");
}
2.After you update each member that attended, do an update on the entire table to increment the total number of raids that have happened:
mssql_query("update table_name set total=(total+1)");
on form:
<input type=checkbox name=selected[] value='" . $f1 . "'>
on raidattend.php:
$selected=$_POST['selected'];
while (list ($key,$val) = #each ($selected)) {
//$val will hold the username
}

Categories