How can I create functions for buttons made in PHP loop? - php

while($record = mysql_fetch_array($myData)){
if(($record['Opponent'] == "") && ($record['better_name'] != $_SESSION['user_name'])){
$acceptID = "accept" . $record['bet_ID'];
echo "<form action=index.php method=post>";
echo "<input type=submit name=" . $acceptID . " value="ACCEPT">";
echo "<input type=hidden id=" . $record['bet_ID'] . ">";
echo "</form>";
} elseif (($record['Opponent'] == "") && ($record['better_name'] == $_SESSION['user_name'])){
echo "<form action=index.php method=post>";
echo "<input type=submit name=delete value=DELETE>";
echo "<input type=hidden id=" . $record['bet_ID'] . ">";
echo "</form>";
} else {
echo "Game Locked.";

I was trying to pass bet_ID's via $_POST['ACCEPT'] and $_POST['DELETE'] commands. Turns out I didn't need forms for each input/button. This is what worked for me:
## $record['bet_ID'] was pulled from an SQL database table.
echo "<input type=hidden name=accept value='$record[bet_ID]'>";
echo "<input type=submit value=Accept>";
$date = $_POST['accept'];
echo $date;

Related

My php code is not Updating or Adding to database

Basically I am not getting any errors when I press update or add. Can anyone help me out? I want it so when I press add it adds that data to the database, and when I press update it updates the database with that value.
<html>
<head>
<title>Subcontractors Data</title>
</head>
<body>
Logout
Homepage
<?php
//make connection
$con = mysqli_connect("localhost","root","");
if(!$con){
die("Can not connect " . mysqli_error());
}
//select db
mysqli_select_db($con , 'subcontractor');
$sql="SELECT * FROM subcontractors";
if(isset($_POST['update'])){
$UpdateQuery = "UPDATE subcontractors SET ID='$_POST[ID]', Name='$_POST[Name]', Surname='$_POST[Surname]', FPA='$_POST[FPA]', Performance='$_POST[Performance]' WHERE ID='$_POST[hidden]'";
mysqli_query($con, $UpdateQuery);
};
if(isset($_POST['add'])){
$AddQuery = "INSERT INTO subcontractors (ID, Name, Surname, FPA, Performance) VALUES ('$_POST[aID]','$_POST[aName]','$_POST[aSurname]','$_POST[aFPA]','$_POST[aPerformance]')";
mysqli_query($con, $AddQuery);
};
$my_Data=mysqli_query($con,$sql);
echo "<table border=1>";
echo"<tr>";
echo"<th>ID</th>";
echo"<th>Name</th>";
echo"<th>Surname</th>";
echo"<th>FPA</th>";
echo "<th>Performance</th>";
echo "</tr>";
while($record=mysqli_fetch_assoc($my_Data)){
echo "<form action=editsub.php method=post>";
echo "<tr>";
echo "<td>" . "<input type=text name='ID' value=".$record['ID'] ." </td>";
echo "<td>" . "<input type=text name='Name' value=".$record['Name'] . " </td>";
echo "<td>" . "<input type=text name='Surname' value=".$record['Surname'] . " </td>";
echo "<td>" . "<input type=text name='FPA' value=".$record['FPA'] . "% </td>";
echo "<td>" . "<input type=text name='Performance' value=".$record['Performance'] . "% </td>";
echo "<input type=hidden name='hidden' value=" . $record['ID'] . ">";
echo "<input type=submit name='update' value='update'>";
echo "</tr>";
echo "</form>";
}
echo "<form action=editsub.php mehtod=post>";
echo "<tr>";
echo "<td><input type=text name='aID'></td>";
echo "<td><input type=text name='aName'></td>";
echo "<td><input type=text name='aSurname'></td>";
echo "<td><input type=text name='aFPA'></td>";
echo "<td><input type=text name='aPerformance'></td>";
echo "<td>" . "<input type=submit name='add' value='add'" . " </td>";
echo "</form>";
echo "</table>";
mysqli_close($con);
?>
</body>
</html>
This way you can check error.
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

I cannot make update on PHP (MySQLi)

There is an error in my php that I cannot solve. Everything is working including 'insert' but not update but I don't know why. If you can help me it would be great. Thank you!
i cannot make update on PHP (Mysqli)
<?php
include("conn.php");
echo "<meta charset='utf-8'>";
?>
<?php
$id = $_GET['edi'];
$showrecs = "SELECT ID, English, Georgian FROM register WHERE ID='$id'";
$result = $conn->query($showrecs);
if(isset($_POST['update'])){
$Updatee = "UPDATE register SET English='".$_POST['English']."', Georgian='".$_POST['Georgian']."' WHERE ID='$id'";
$res=mysqli_query($conn, $Updatee);
if($res==1){
echo "წარმატებით განხორციელდა რედაქტირება.";
echo "<br>";
echo "<a href='view.php'>ჩანაწერების ნახვა</a>";
}
if($res==0){
echo "არაფერიც არ მოხდა";
}
exit();
} elseif ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<form method=POST>";
echo "<tr>";
echo "<td>" . "<input type='text' name='id' value=" . $row['ID'] ." disabled>". "</td>";
echo "<td>" . "<input type='text' name='English' value=" . $row['English'] .">". " </td>";
echo "<td>" . "<input type='text' name='Georgian' value=" . $row['Georgian'] .">". " </td>";
echo "<td>" . "<input type='submit' name='update' value='შეცვლა'>" . " </td>";
echo "</form>";
echo "</br>";
echo "</br>";
}
} else {
echo "Error;";
}
$conn->close();
?>

php mysql leading my update and delete page to homepage

these are the codes
where do you think I should insert the header (Location:'home.php')
I tried using after the if statements, these are the codes
else {
try {
header('Location: home.php');
}
but it directly goes to home.php even when I haven't clicked the update button
<?php
if (isset($_POST['update'])){
$UpdateQuery = "UPDATE registry SET db_id = '$_POST[db_id]', db_name =
'$_POST[db_name]', db_age = '$_POST[db_age]', db_gender =
'$_POST[db_gender]', db_birthdate = '$_POST[db_birthdate]', db_phone =
'$_POST[db_phone]', db_address = '$_POST[db_address]'
WHERE db_id = '$_POST[hidden]'";
mysql_query($UpdateQuery);
}
$sql = "SELECT * FROM registry";
$query = mysql_query($sql) or throw_ex(mysql_error()); ;
echo"<h2><center>Employee Masterlist</center></h2>";
echo "<table class = \"form\" border = \"1\" cellspacing = \"3\">";
echo "<tr>";
echo "<th>Employee ID</th>";
echo "<th>Name</th>";
echo "<th>Age</th>";
echo "<th>Gender</th>";
echo "<th>Birthdate </th>";
echo "<th>Phone No. </th>";
echo "<th>Address </th>";
echo "</tr>";
while($row = mysql_fetch_array($query))
{
echo "<form action=update.php method=post>";
echo "<tr>";
echo "<td>" . "<input id=input-up type=text name=db_id
value=" . $row['db_id'] . "> </td>";
echo "<td>" . "<input id=input-up type=text name=db_name
value=" . $row['db_name'] . "> </td>";
echo "<td>" . "<input id=input-up type=text name=db_age
value=" . $row['db_age'] . "> </td>";
echo "<td>" . "<input id=input-up type=text
name=db_gender value=" . $row['db_gender']. "> </td>";
echo "<td>" . "<input id=input-up type=text
name=db_birthdate value=" . $row['db_birthdate'] . "> </td>";
echo "<td>" . "<input id=input-up type=text
name=db_phone value=" . $row['db_phone'] . "> </td>";
echo "<td>" . "<input id=input-up type=text
name=db_address value=" . $row['db_address'] . "> </td>";
echo "<input type=hidden name=hidden value=" .
$row['db_id'] . "> </td>";
echo "<td>" . "<input class=send_btn type=submit
name=update value=Update". "> </td>";
echo "</tr>";
echo "</form>";
}
echo"</table>";
mysql_close();
?>
</div>
</body>
</html>
You should add it after query is executed.
Here is the code
if (isset($_POST['update'])){
$UpdateQuery = "UPDATE registry SET db_id = '$_POST[db_id]', db_name =
'$_POST[db_name]', db_age = '$_POST[db_age]', db_gender =
'$_POST[db_gender]', db_birthdate = '$_POST[db_birthdate]', db_phone =
'$_POST[db_phone]', db_address = '$_POST[db_address]'
WHERE db_id = '$_POST[hidden]'";
mysql_query($UpdateQuery);
//redirect to home.php
header('Location: home.php');
}

Update is not working in database

Dont mind the security issues, this is just local testing, but when ever i click the update button none of the changes go through on the page or on the query and i get no erros.
<?php
$link = mysqli_connect("localhost", "root", "", "test") or die("could not connect");
if (isset($_POST['update'])) {
$updateQuery = (" UPDATE `test1` SET f_name = '$_POST[f_name]', l_name='$_POST[l_name]', email='$_POST[email]' WHERE id='$_POST[id]'");
mysqli_query($link, $updateQuery);
};
$query = ("SELECT * FROM `test1`");
$result = mysqli_query($link, $query);
echo "<table border=1
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<form method=post action=update.php>";
echo "<tr>";
echo "<td>" . "<input type=text name=f_name value=" . $row['f_name'] . " </td>";
echo "<td>" . "<input type=text name=l_name value=" . $row['l_name'] . " </td>";
echo "<td>" . "<input type=text name=email value=" . $row['email'] . " </td>";
echo "<td>" . "<input type=hidden name=id value=" . $row['id'] . " </td>";
echo "<td>" . "<input type=submit name=submit value=update" . " </td>";
echo "</tr>";
}
?>
change your form to
while($row = mysqli_fetch_array($result)) {
echo "<form method=post action=update.php>";
echo "<input type=hidden name=update>";
echo "<tr>";
echo "<td>" . "<input type=text name=f_name value=" . $row['f_name'] . " </td>";
echo "<td>" . "<input type=text name=l_name value=" . $row['l_name'] . " </td>";
echo "<td>" . "<input type=text name=email value=" . $row['email'] . " </td>";
echo "<td>" . "<input type=hidden name=id value=" . $row['id'] . " </td>";
echo "<td>" . "<input type=submit name=submit value=update" . " </td>";
echo "</tr>";
}
POST keys should be in quotes. Try this instead:
$updateQuery = "UPDATE test1 SET f_name = ".$_POST['f_name'].", l_name=."$_POST['l_name'].", email=".$_POST['email']." WHERE id=".$_POST['id'];
Try this:
$updateQuery = ("UPDATE `test1` SET f_name = '{$_POST['f_name']}', l_name='{$_POST['l_name']}', email='{$_POST['email']}' WHERE id='{$_POST['id']}'");
Also you can try echoing something inside your if (isset($_POST['update'])) { to make sure it is testing true.
Here is your problem:
if (isset($_POST['submit']) && $_POST['submit'] == 'update') {
The name of the submit button is submit not update, the value is update.

Selecting values from the database and make the checkbox checked if it does exist

//query to check if part id number exists in table ATTEND where service id = ...
$result2 = mysql_query("SELECT * FROM attend WHERE SIDno='$SIDno' and ServiceID='$id");
//if exists $ok = true;
if (mysql_num_rows($result2)>0) {
$ok == true;
}
echo "<tr bgcolor=$bgcolor>";
echo "<td><a name=$row1[0] id=$row1[0]>$row1[0]</td>";
echo "<td>" . $row1[1] . "</td>";
echo "<td>" . $row1[5] . "</td>";
echo "<td>" . $row1[2] . "</td>";
echo "<td>" . $row1[3] . "</td>";
echo "<td><input type='checkbox' name='checkbox[]' value=" . $row1[0];
if ($ok == true) {
echo 'disabled="disabled" checked="checked"';
}
echo "></td>";
echo "<input type='hidden' name='ServiceID' value=" . $id . ">";
echo "<input type='hidden' name='Year' value=" . $Year . ">";
echo "<input type='hidden' name='Stype' value='Recollection'>";
echo "</tr>";
}
}
echo "<tr>
<td colspan='5' align='right' bgcolor='#FFFFFF'><input name='SUBMIT1' type='submit' id='SUBMIT'value='SUBMIT'></td>
</tr>";
How can implement that on the next load if the value of the checkbox is already available in the database it will now be checked. but if it is not yet existing i can check it and save it to the database.
I'm guessing the problem lies in this line:
if (mysql_num_rows($result2)>0) {
$ok == true;
}
It should be:
if (mysql_num_rows($result2)>0) {
$ok = true;
}
In the first snippet you are just testing if $ok is equal to true, while in the second example an actual assignment to the variable is performed.
Remember:
= != ==

Categories