PHP : How to pass get value in url - php

I want to pass both stock (user input) and id in one url one I try this I get Undefined index: stock is the way I'm passing the input value wrong ?
<?php
session_start();
$sql1 = "SELECT * FROM tbl_customers WHERE customers_id='1'";
$sql = "SELECT * FROM tbl_products";
$get = mysqli_query($conn, $sql1) or die(mysqli_error($conn));
$row = mysqli_fetch_array($get);
$customerName = $row['customer_email'];
echo "Welcome $customerName";
$data= mysqli_query($conn, $sql) or die(mysqli_error($conn));
while ($row = mysqli_fetch_assoc($data)) {
$pid = $row['pid'];
echo "<form action='' method='GET'>
<input type=text name=stock value=1><br>
<a href='stock.php?id=" . $pid . "?stock=" . $_GET['stock'] . "'> Add</a>
</form>";
}
?>
thank you everyone for your help this is what I did to fix the error my get value is always empty If I don't press enter
echo "<form action='' method='GET'>
<input type=text name=stock value=1><br>
</form>";
$_GET['stock'] = 1;
if (isset($_GET["stock"])) {
$x = $_GET['stock'];
echo "<a href='stock.php?id=" . $pid . "&stock=" . $_GET['stock'] . "'> Add</a>";
}

Just change ? with & before stock.
echo "<form action='' method='GET'><input type=text name=stock value=1><br>
<a href='stock.php?id=" . $pid . "&stock=" . $_GET['stock'] . "'> Add</a> </form>";

<a href="/php/event-detail.php?event_id=$event_id">
this how you use to pass values to another page. you can refer this http://w3schools.invisionzone.com/index.php?showtopic=48611

Related

How should I make quiz with one answer for each question in php?

I'm trying to make a plain php quiz app. I'm trying to show all questions with different answers. The problem is, when I click on 1st questions answer radio button gets checked, but when I select 2nd questions answer, it's radio button gets checked but the one from 1st questions dissappears. I want to make a form that lets user select one answer from each question and then passes the data to test_calc.php file for processing. Here's the code:
<?php
$connection = mysqli_connect("localhost", "root", "", "vartvald");
if (isset($_GET['id'])) {
$fk = mysqli_real_escape_string($connection, $_GET['id']);
if ($connection->connect_error) {
die("Connection failed:" . $connection->connect_error);
} else {
$sql = "SELECT * FROM questions WHERE fk_test='$fk'";
}
$result = $connection->query($sql);
if ($result->num_rows > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$id = $row['id'];
$question = $row['question'];
$mark = $row['mark'];
$answer_1 = $row['answer_1'];
$answer_2 = $row['answer_2'];
$answer_3 = $row['answer_3'];
$answer_4 = $row['answer_4'];
$correct = $row['correct'];
echo "<div><form method='POST' action='test_calc.php?id=".$fk."'><br><b>" . $row['question'] . "</b><br><input type='radio' name='answer[]' value=".$answer_1.">"
. $row['answer_1'] . "<br><input type='radio' name='answer[]' value=".$answer_2.">" . $row['answer_2'] . "<br><input type='radio' name='answer[]' value=".$answer_3.">" . $row['answer_3'] .
"<br><input type='radio' name='answer[]' value=".$answer_4.">" . $row['answer_4'] . "<input type='hidden' name='mark' value=".$row['mark'].">
<input type='hidden' name='correct[]' value=".$row['correct'].">
<input type='hidden' name='id' value=".$row['id']."><br><br></div>";
}
echo '<button type="submit"><b>Baigti testa</b></button>';
} else {
echo "</table><h2 style='text-align:center'>Testas neturi sukurtu klausimu..</h2>";
}
}
All radio buttons are in the same group: answer[]
You need a separate name for each question, for example like this:
echo "... <input type='radio' name='answer_".$id."[]' value=".$answer_4."> ..."
Set unique name for each questions in loop

Delete individual lines/rows with PHP button

What I am trying to do is have a PHP page display the contents of a MySQL database and and each line it displays, it give me a delete button so I can delete individual rows. I have the code kind of working. The code delete the lines of code, but not the one I select, instead it deletes the last line and not the one I tell it to delete.
Here is a screenshot, I know it does not look pretty, I was going to clean it up after I get the code working.
HTML:
<?php
// Delete php code
if(isset($_POST['delete_series']))
{
// here comes your delete query: use $_POST['deleteItem'] as your id
$delete = $_POST['delete_series'];
$delete_sql = "DELETE FROM `Dropdown_Series` where `id` = '$delete'";
if(mysql_query($delete_sql, $conn))
{
echo "Row Deleted </br>";
echo "$delete";
};
}
//Insert Code
if(isset($_POST['add_series']))
{
$insert_sql = "INSERT INTO dropdown_series (series) VALUES ('$_POST[add_series]' )";
if(!mysql_query($insert_sql, $conn))
{
die ('Error: ' . mysql_error());
}
echo "Series Added <br><br>";
}
?>
<br />
<h1>Add New Series Title</h1>
<h3>Add New: </h3><br />
<form name = action="add_series.php" method="POST">
<input type = "text" name = "add_series" required/><br /><br />
<input type ="submit" name="submit" value="Add">
</form>
<h3>Current Series: </h3><br />
<?php
//Delete button on each result of the rows
$query = mysql_query("SELECT id, series FROM Dropdown_Series"); // Run your query
// Loop through the query results, outputing the options one by one
echo "<form action='' method='POST'>";
while ($row = mysql_fetch_array($query))
{
echo $row['series'] . " ";
echo $row['id'];
echo "<input type='hidden' name='delete_series' value=' " . $row['id'] . "' />";
echo "<input type='submit' name='submit' value='Delete'>";
//echo $row['series'] . " ". $row['id'] . "<input type='submit' value='delete'>";
echo "<br />";
}
echo "</form>";
?>
Your loop will result in multiple delete_series inputs - within the same form.
You could create separat forms for each option:
while ($row = mysql_fetch_array($query))
{
echo "<form action='' method='POST'>";
// ...
echo "<input type='hidden' name='delete_series' value=' " . $row['id'] . "' />";
echo "<input type='submit' name='submit' value='Delete'>";
// ...
echo "</form>";
}
I believe that a very quick answer would be
while ($row = mysql_fetch_array($query))
{
echo "<form action='' method='POST'>";
echo $row['series'] . " ";
echo $row['id'];
echo "<input type='hidden' name='delete_series' value=' " . $row['id'] . "' />";
echo "<input type='submit' name='submit' value='Delete'>";
//echo $row['series'] . " ". $row['id'] . "<input type='submit' value='delete'>";
echo "<br />";
echo "</form>";
}
If I were you I would prefer injecting some javascript in the logic, and perform the Delete request using an ajax call.
you can refer to this tutorial to get more understanding of what I am refering to https://scotch.io/tutorials/submitting-ajax-forms-with-jquery

Selectbox for update value

I have a selectbox in PHP, where the values and one value is selected. This selected value is data from table in MYSQL. Now, I need update/change this value to another which is in selectbox and send new value in table. When I send this new value from selectbox nothing happens. Can someone write how to update values ​​with selectbox.
<?php echo "<form action=zap_azuriraj.php?y=".$_GET['y']."&b=".$_GET['b']." method=POST>";
echo $_GET['y'];
$upit = "SELECT * FROM zapis WHERE zapis_id='$_GET[y]'";
$rezultat = izvrsiUpit($upit);
$rows = mysql_fetch_array($rezultat);
$id_bilj=$rows['biljka_id'];
$upit2 = "SELECT * FROM biljka";
$rezultat1 = izvrsiUpit($upit2);
if(isset($_GET['b'])) {
echo"<select name='biljkaa_id'>"; }
while ($row = mysql_fetch_array($rezultat1)){
if ($row['biljka_id']==$id_bilj) {
$optional="selected=\"selected\"";
}
else {
$optional="";
}
echo "<option $optional value=".$row['biljka_id'].">".$row['naziv']." </option>";
}
echo"</select>";
others are all working
echo"<h3> Datum:</h3>";
echo"<input type=date name=date value=" . $rows[3] . ">";
echo "<h3> Vrijeme:</h3>";
echo"<input type=time name=vrijeme value=" . $rows[4] . ">";
echo"<h3>Opis:</h3>";
echo"<textarea name=opis >" . $rows[5] . "</textarea>";
echo"<h3> Broj parcele:</h3>";
echo"<input type=text name=br_parcele value=" . $rows[6] . ">";
echo"<h3> Broj biljke:</h3>";
echo"<input type=text name=br_biljke value=" . $rows[7] . ">";
?>
<input type="submit" value="update" name="update">
</form>
<?php
I think the problem is in the commented line
if(isset($_POST['update'])&&(isset($_POST['biljkaa_id']))){
/* $biljka_id = $row['biljka_id'];*/
$datum=$_POST['date'];
$vrijeme = $_POST['vrijeme'];
$opis = $_POST['opis'];
$br_parcele = $_POST['br_parcele'];
$br_biljke=$_POST['br_biljke'];
$id=$_GET['y'];
$upit="UPDATE zapis SET
biljka_id='$biljka_id',
datum='$datum',
vrijeme='$vrijeme',
opis='$opis',
broj_parcele = '$br_parcele',
broj_biljke = '$br_biljke'
WHERE zapis_id='$id'";
$rezultat = izvrsiUpit($upit);
echo "Ažuriran je zapis";
}
?>
I found a solution , thanks anyway
$biljka_id = $_POST['biljkaa_id'];

php: Using <form> tag to sum a new value to an existing value in php

I am creating a webpage that is similar to a points system. It consists of a table with name and points columns. The user inputs a number, which then adds that value to the existing number in the table. My question is how would I be able to add those two values and update the table(database)?
<?php
$con = mysql_connect("xxx, xxx, xxx);
if (!$con) {
die("can not connect:" . mysql_error());
}
mysql_select_db("points", $con);
if(isset($_POST['update'])){
$UpdateQuery = "UPDATE coach_tbl set coachscore = coachscore + '$add' WHERE coach_score = '$_POST[hidden]'";
mysql_query($UpdateQuery, $con);
};
if (isset($_POST['submit'])){
$AddQuery = "INSERT INTO coach_tbl (coach_name, coach_score) VALUES('$_POST[name]', '$_POST[score]')";
mysql_query($AddQuery, $con);
};
$sql = "SELECT * FROM coach_tbl ORDER BY coach_score DESC";
echo "<table border=1>
<tr>
<th>NAME</th>
<th>Score</th>
</tr>";
$myData = mysql_query($sql, $con);
while($record = mysql_fetch_array($myData)) {
echo "<form action=index.php method=post>";
echo "<tr>";
echo "<td><input type=text name=coachname value='" . $record['coach_name'] . "'> </td>";
echo "<td><input type=text name=coachscore value='" . $record['coach_score'] . "'> </td>";
echo "<td><input type=hidden name=hidden value='" . $record['coach_score'] . "'> </td>";
echo "<td><input type=submit name=update value=update'" . "'> </td>";
echo "<td><input type=number min="1" max="10" name=add value=add'" . "'> </td>";
echo "</tr>";
echo "</form>";
}
echo "</table>";
mysql_close($con);
?>
If there are any questions I will gladly elaborate on anything. I am also fairly new to php.
Let's say you have an html form like so:
<form action="update.php" method="POST">
<input type="number" name="updateData" value=""/>
<input type="submit">
</form>
and an update.php:
<?php
//assuming you want 1-10 points max to be added each time
if( isset($_POST['updateData']) && $_POST['updateData'] >=1 && $_POST['updateData'] >=10){
//set to user inputed value
$insertData = $_POST['updateData'];
$sql = "UPDATE point_table SET points = points + $insertData WHERE id = 1";
//you will need to finish off the query by checking connecting with your database.
}
?>

PHP Session Getting CorrectValue

Question: What to do to fix my problem on handling the session because it is returning an incorrect value.
Situation: I'm having problem on this session variable from the table. I added data from database to a table using while loop. Here is my code:
<form action="edit2.php" method="get">
<?php
$link = mysql_connect("localhost", "root", "root");
mysql_select_db("ispot", $link);
$result4 = mysql_query("SELECT * FROM user_ispot", $link);
$num_rows = mysql_num_rows($result4);
$result = mysqli_query($con,"SELECT * FROM complaints");
echo "<table border='1'>
<tr>
<th>Id Number</th>
<th>Category</th>
<th>Problem</th>
<th>Date Reported</th>
<th>Complaint ID </th>
<th>Action</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td name=id_num>" . $row['id_number'] . "</td>";
$_SESSION['favcolor'] = "$row[id_number]";
echo "<td name=remarks>" . $row['remarks'] . "</td>";
echo "<td name=status>" . $row['status'] . "</td>";
echo "<td name=date>" . $row['date_reported'] . "</td>";
echo "<td>" . "<INPUT TYPE = text Name = cid VALUE = " . $row['complaint_id'] . ">" . "</td>";
echo "<td>" . "<INPUT TYPE = Submit Name = Submit1 VALUE =Edit>" . "</td>";
echo "</tr>";
}
echo "</table>" ;?>
And it looks like this:
As you can see, there is the edit button, where I can edit a specific row in the table.
When I press the edit button, this will show:
Notice that the User ID is wrong, what can I do to fix it? because the user id that is being post here was the last user_id that was inserted in the table.
And here is my code for the second image:
<b>Date:</b> <input type='text' name='today' placeholder='<?php echo $today ?>' disabled='disabled'> <br><br>
<b>User ID:</b> <input type='text' disabled='disables' name='userid' placeholder='<?php
//$comid = $_GET["cid"];
//echo $userid;
echo $_SESSION['userid'];
//$result = mysqli_query($con,"SELECT * FROM complaints WHERE id = XXX");
//$row = mysqli_fetch_assoc($result);
//print_r($row);
//$result2 = mysql_query("SELECT * FROM complaints WHERE complaint_id = '$comid'", $link);
//$result2 = mysql_query("SELECT * FROM complaints", $link);
//while($row = mysql_fetch_assoc($result2))
//{
//echo $row['id_number'];
//}
?>'></br><br>
Any help would be appreciated. Thank you.
i replaced the button with a link, used it to pass the value when edit is clicked, catch the value with a get and it works for me.
in edit.php
echo "<td> <a href = 'edit2.php?id=$num_id'>Edit</a></td>";
in edit2.php
$id = $_GET['id'];
<b>User ID:</b> <input type='text' disabled='disables' name='userid' value = '<?php echo $id;?>'></input type>
$result2 = mysql_query("SELECT * FROM complaints WHERE complaint_id = '$comid'", $link);
$result2 = mysql_query("SELECT * FROM complaints", $link);
You must use just one of them this rows. I think problem is the second row. This query not choose the "id" that is "comid".
Your first query row is enough:
$result2 = mysql_query("SELECT * FROM complaints WHERE complaint_id = '$comid'", $link);

Categories