Change a specific mysql column - php

So I've got this code, which changes my type column from a to d in my mysql database. But sadly, it changes even all the other type columns of all the other user ALTHOUGH I specified it with WHERE id_user = X(in the other codes, only the WHERE id_user = X varies like WHERE id_user = 1, WHERE id_user = 2 etc.):
<td><?php
if(isset($_POST['d'])){
$var2 = $_POST['d'];
$sql = "UPDATE users SET `type`= '$var2' WHERE id_user = 2 ";
$mysqli->query($sql) or die($mysqli->error);
}
if(isset($_POST['a'])){
$var2 = $_POST['a'];
$sql = "UPDATE users SET `type`= '$var2' WHERE id_user = 2 ";
$mysqli->query($sql) or die($mysqli->error);
}
?>
<?php
if ($result = $mysqli->query("SELECT `type` FROM users WHERE id_user = 2 ")) {
$r = mysqli_fetch_assoc($result);
}
echo implode($r);
echo implode($_POST);
?>
</td>
<td>
<form action="#" method="POST">
<input type="hidden" value="d" name="d"/>
<input type="submit" value="--Inaktiv--"/>
</form>
<form action="#" method="POST">
<input type="hidden" value="a" name="a"/>
<input type="submit" value=" --Aktiv-- "/>
</form>
Why does this happen and how do I change it the way, only the column of the right user changes? How do I determine the submit button to the specific $sql = "UPDATE users SETtype= '$var2' WHERE id_user = X "; ?

Related

How can I access a user's answer for my quiz on a different PHP page?

I currently have a simple quiz on my page quiz_v1.php with 3 possible answers and a submit button. The answer is posted to a separate page, quiz_dest.php, where it currently displays the value for the answer ID (A, B or C).
quiz_v1.php
<form action="quiz_dest.php" method="post" id="quiz">
<ol>
<li>
<!-- display question as heading -->
<?php
while ( $row = $result->fetch_assoc() ) {
echo "<h3>" . $row[ "question" ] . "</h3>";
}
?>
<!-- display answers as radio -->
<div>
<input type= "radio" name="question1_answers" id="question1_answers_A" value="A"/>
<label for="question1_answers_A">
<?php while($row = $result2->fetch_assoc()) {echo $row["choice_text"];}?>
</label>
</div>
<div>
<input type= "radio" name="question1_answers" id="question1_answers_B" value="B"/>
<label for="question1_answers_A">
<?php while($row = $result3->fetch_assoc()) {echo $row["choice_text"];}?>
</label>
</div>
<div>
<input type= "radio" name="question1_answers" id="question1_answers_C" value="C"/>
<label for="question1_answers_A">
<?php while($row = $result4->fetch_assoc()) {echo $row["choice_text"];}?>
</label>
</div>
</li>
</ol>
<input type="submit" value="Submit answer"/>
</form>
I use the SQL statements below to retrieve the information from my DB.
//queries
$sql = "SELECT question FROM Question WHERE question_id = '1'";
$result = $conn->query( $sql );
$sql2 = "SELECT choice_text FROM Question_choices WHERE choice_id = '1' AND question_id = '1'";
$result2 = $conn->query( $sql2 );
$sql3 = "SELECT choice_text FROM Question_choices WHERE choice_id = '2' AND question_id = '1'";
$result3 = $conn->query( $sql3 );
$sql4 = "SELECT choice_text FROM Question_choices WHERE choice_id = '3' AND question_id = '1'";
$result4 = $conn->query( $sql4 );
In quiz_dest.php
$answer1 = $_POST[ "question1_answers" ];
echo "$answer1";
I would like to be able to assign to a variable the user's choice_id, carry this over to quiz_dest and use a SQL query to compare the choice_ID with is_right_choice to see if it is the correct answer and then display a message to the user.
I can't quite wrap my head around how I could use $_POST and $_GET to achieve this so any help would be greatly appreciated.
I have also included a screenshot below of the table in my database.
Screenshot
Use the $_GET[] method. If you are using GET than the answer should appear in the URL if the answer goes to another PHP page. You can also try adding the answers to a database and implement some MySQL.

How to set unique id on html form in while loop

How I can set unique id for html form which is in while loop, and get this id when check is the form is submit? You can check out my code:
while($row_lessons = mysqli_fetch_assoc($queryLessons)) {
echo 'someinfohere';
if(isset($_POST['buy'])) {
mysqli_query($link, "UPDATE users SET credits = credits - ".$row_lessons['price']." WHERE id = '".$_SESSION['userid']."' ");
mysqli_query($link, "INSERT INTO `lessonpurchases` (lesson_id, user_id, date) VALUES ('".$row_lessons['id']."', '".$_SESSION['userid']."', '".time()."')") or die(mysqli_error($link));
}
echo '
Цена: '.levche($row_lessons['price']).'лв. <br />
<form action="" method="post">
<input type="submit" name="buy" value="Купи" />
</form>';
}
The unique id is needed, because when submit the form the mysql queries are being executed for all results in loop.
If you want to keep it simple:
$i = 1;
while($row_lessons = mysqli_fetch_assoc($queryLessons)) {
echo 'someinfohere';
if(isset($_POST['buy'.$i])) {
mysqli_query($link, "UPDATE users SET credits = credits - ".$row_lessons['price']." WHERE id = '".$_SESSION['userid']."' ");
mysqli_query($link, "INSERT INTO `lessonpurchases` (lesson_id, user_id, date) VALUES ('".$row_lessons['id']."', '".$_SESSION['userid']."', '".time()."')") or die(mysqli_error($link));
}
echo '
Цена: '.levche($row_lessons['price']).'лв. <br />
<form action="" method="post">
<input type="submit" name="buy'.$i.'" value="Купи" />
</form>';
$i++;
}
You can use uniqid() as follow
$formId = uniqid();
In your form you can use it like
<form action="" method="post" id="<?php echo $formId;?>">
<input type="hidden" name="formId" value="<?php echo $formId;?>" />
<input type="submit" name="buy" value="Купи" />
</form>
On form submission you will get the formId by using $_REQUEST['formId']

Increment id on click in MySQL

I have a problem to visualize the solution for the problem that I have now.
The user is allowed to insert a row in a table.
And I try to display a button (input) +1 who allow the user to increment a column (vote) in a selected row among all created.
The problem is that I don't get the thing for rely incrementation to the desired id.
Here my code :
<form action="" method="post">
<input type="text" name="disease">name
<input name="mainsubmit" type="submit" value="submit">
</form>
</body>
</html>
<?php
if(isset($_POST['mainsubmit']))
{
$nameDisease = $_POST['disease'];
$req = $db->prepare('INSERT into disease(name) VALUES(:name)');
$req->execute(array('name' => $nameDisease));
}
$query = $db->query('SELECT * FROM disease');
while ($result = $query->fetch())
{
$id = $result['id'];
echo $id ?>
<form action="" method="post"> <input name="secondsubmit" type="submit" value="+1"> </form><?php
if(isset($_POST['secondsubmit']))
{
$db->exec("UPDATE disease SET vote = vote + 1 WHERE id = " .$id);
}
}
Logically, the code above doesn't work but I don't understand how find the solution.
In brief, i want to allow the user to increment a column in a selected row.
Thanks
Edit: Shadow, it's not my problem because your solution is used for automatically chose between INSERT or UPDATE if the line doesn't exist or exist. Me, I want allow the user to create rows and allow he to vote +1 on each of one that exist, and it will not be possible for he to insert a row from the input +1.
I created code snippet similar to your code style.
You have two submit buttons so you need to separate handling of those two requests.
The $id of the item you want to update in the second submit need's to come from hidden value in form.
In order for this to work you need to create table in mysql:
create table disease (id MEDIUMINT NOT NULL AUTO_INCREMENT, name VARCHAR(20), vote INTEGER, PRIMARY KEY (id)); - for example like this
<html>
<body>
<form action="" method="post">
<input type="text" name="disease">name
<input name="mainsubmit" type="submit" value="submit">
</form>
</body>
</html>
<?php
$db = new PDO('mysql:dbname=phpapp;host=db', 'root', 'phpapptest');
if (isset($_POST['mainsubmit'])) {
$nameDisease = $_POST['disease'];
$req = $db->prepare('INSERT into disease (name, vote) VALUES(:name, 0)');
$req->bindParam(':name', $nameDisease);
$req->execute();
$query = $db->query('SELECT * FROM disease');
while ($result = $query->fetch()) { ?>
<form action="" method="post">
<p><?php echo $result['name'] . " : " . $result['vote'];?>
<input name="secondsubmit" type="submit" value="+1" />
<input type="hidden" name="id" value="<?php echo $result['id'];?>" />
</p>
</form>
<?php }
}
if (isset($_POST['secondsubmit'])) {
$req = $db->prepare("UPDATE disease SET vote = vote + 1 WHERE id = " . $_POST['id']);
$req->execute();
$query = $db->query('SELECT * FROM disease');
while ($result = $query->fetch()) {?>
<form action="" method="post">
<p><?php echo $result['name'] . " : " . $result['vote'];?>
<input name="secondsubmit" type="submit" value="+1" />
<input type="hidden" name="id" value="<?php echo $result['id'];?>" />
</p>
</form>
<?php }
}
?>

SQL Update Query not updating records

I have a list of games that when clicked lead to a form that allows users to edit records before updating them.
Here is the Edit Games page;
<?php
$gameID = isset($_GET['gameID']) ? $_GET['gameID'] : '';
$gameYear = isset($_GET['gameYear']) ? $_GET['gameYear'] : '';
$gamePrice = isset($_GET['gamePrice']) ? $_GET['gamePrice'] : '';
$gameName = isset($_GET['gameName']) ? $_GET['gameName'] : '';
$sql = "SELECT * FROM game WHERE gameName = $gameName";
$queryresult = mysqli_query($conn, $sql)
or die (mysqli_error($conn));
$row = mysqli_fetch_assoc($queryresult);
$gameID = $row['gameID'];
$gameYear = $row['gameYear'];
$gamePrice = $row['gamePrice'];
$gameName = $row['gameName'];
?>
<div id="form">
<form action="updateGame.php" id="Update" method="get">
<label> Game ID
<input id="text" name="id" value = "<?php echo $gameID; ?>" />
</label>
<label> Year
<input type = "text" name = "year" value = "<?php echo $gameYear; ?>" />
</label>
<label> Price
<input type = "text" name = "price" value = "<?php echo $gamePrice; ?>" />
</label>
<label> Name
<input type = "text" name = "name" value = "<?php echo $gameName; ?>" />
</label>
<input type = "submit" value = "Update">
Here is the update page that runs when the submit button is clicked;
<?php
$gameID = isset($_GET['gameID']) ? $_GET['gameID'] : '';
$gameYear = isset($_GET['gameYear']) ? $_GET['gameYear'] : '';
$gamePrice = isset($_GET['gamePrice']) ? $_GET['gamePrice'] : '';
$gameName = isset($_GET['gameName']) ? $_GET['gameName'] : '';
$sql = "UPDATE game SET gameYear = '$gameYear', gamePrice = '$gamePrice', gameName = '$gameName' WHERE gameID = '$gameID'";
mysqli_query($conn, $sql)
or die (mysqli_error($conn));
mysqli_close($conn);
echo "Updated";
?>
The edit games page works and I can edit the records, when I press the submit button it comes up with the echo statement saying the record was updated, however, no changes happen and I'm not sure why.
Your form uses name="id", not name="gameID", so your $_GET['gameID'] variable is not set on any of your requests, but $_GET['id'] is. Update your form or your GET page, so that the names match.
You've forgot to append the variables in your query. I should be like this:
$sql = "UPDATE game SET gameYear = '".$gameYear."', gamePrice = '".$gamePrice."', gameName = '".$gameName."' WHERE gameID = '".$gameID."'";

using multi where when select items from database

I want to selected items from mytable when using three tables and &_GET another id to open in this page so i want to use where and where to complete fetch my data by using two roles .
<?php
$sel = "SELECT * FROM `informations` where `cate_id` =".$_GET['info_id'];
$done = mysql_query($sel);
?>
<form action="" method="post" enctype="multipart/form-data">
<label for="location"></label>
<select name="location" id="location"><?php
$sel_cate = "SELECT * FROM locations";
$done_cate = mysql_query($sel_cate);
while($get_cate = mysql_fetch_array($done_cate)){
echo '<option value="'.$get_cate['id'].'">'.$get_cate['location'].'</option>';
$loc=$get_cate['id'];
}
?>
</select>
<input type="submit" name="go" id="go" value="Go">
<input type="submit" name="all" id="all" value="Show All...">
</form>
<?php
if(isset($_POST['go'])){
$sel ='SELECT * FROM `pharmacies` WHERE `cate_id` ="'.$_GET['info_id'].'" || `location_id` = "'.$_POST['location'].'"';
?>
I tried this code and when isset($_POST['go']) variable $sel got $_GET['info_id'] and $_POST['location'] values. Query generated without errors, and must fetch information.
I not see mysql_query in your: if(isset($_POST['go'])). Maybe you forget query:
if(isset($_POST['go']))
{
$sel = 'SELECT * FROM `pharmacies` WHERE `cate_id` ="'.addslashes($_GET['info_id']).'" or `location_id` = "'.addslashes($_POST['location']).'"';
$selRslt = mysql_query($sel);
while($row = mysql_fetch_array($selRslt))
{
var_dump($row);
}
}

Categories