Trying:
take info from my database to texfields (infinite database data so cant name fields in hardcore mode each by each), then change data and put it back to database.
Problem:
Can't name my textfields in order to check if data was changed.
Code:
<html>
<head>
<title>Data</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<?php
$conn = mysql_connect('localhost', 'root', '');
$base = 'CREATE DATABASE itemsbase';
$select = mysql_select_db("itemsbase",$conn);
$table_items = 'CREATE TABLE `items` (
`items_id` int UNIQUE NOT NULL,
`shop_id` int,
`itemname` varchar(40),
`price` varchar(50),
PRIMARY KEY(items_id))';
$query = "SELECT * FROM items";
$result = mysql_query($query);
if(!$select){
if(mysql_query($base, $conn)){
echo "base created";
}else{
echo 'error: ' . mysql_error() . "\n";
}}
if(mysql_query($table_items, $conn)){
echo "table created";
}else{
echo 'error: ' . mysql_error() . "\n";
}
echo "<form method='post'><table>";
echo "<tr><td>" . 'item id' . "</td><td>" . 'shop id' . "</td><td>" . 'item name' . "</td><td>" . 'price' . "</td></tr>";
while($row = mysql_fetch_array($result)){
echo "<tr><td><input type='text' name='itemid" . $row[1] . "' value='" . $row['items_id'] . "'></td><td><input type='text' name='shop_id" . $row[1] . "' value='" . $row['shop_id'] . "'></td><td><input type='text' name='itemname" . $row[1] . "' value='" . $row['itemname'] . "'></td><td><input type='text' name='price" . $row[1] . "' value='" . $row['price'] . "'></td></tr>";
//and here trying take info from field and check if it mach with info in database
$fieldinfo = $_POST['itemid' . $row[1]];
if($fieldinfo=$row['items_id']){
}else{
$row['items_id']=$fieldinfo;}
//but facing error undefined index
}
echo "</table></form>";
mysql_close ();
?>
</body>
</html>
name the text field as an array with the record ID so you will be able to relate that back to the data base.
<input type='text' name='item[<?=$row['id']?>]' value='<?=$row['value']'>
or you can add a custom attribute to the text feild with the table record ID.
<input type='text' name='item' rowid='<?=$row['id']?>' value='<?=$row['value']?>'>
either way can be reference by jquery to check for update and update database.
Related
I have a table with some information from my database. I have buttons next to each row in the table. If the button is clicked it should send an email to the user. I can give each button a unique value but can not have this value in the if isset.... How should i get the value of the button name to be variable and how could i get the value at the end of my url.
echo "<table>";
echo "<tr><td>Naam</td><td>EmailAdress</td><td>Datum</td><td>Type</td><td>Producent</td><td>SerieNummer</td><td> imei</td><td>Manager</td><td>bevestigd</td></tr>";
while($row = mysqli_fetch_row($result)){
$sqlid = "SELECT Bevestigd FROM tbl_bevestiging WHERE IDbewijs = " . $row[0];
$resultid = mysqli_query($conn, $sqlid);
$row2 = mysqli_fetch_row($resultid);
echo "<tr><td>" . $row[1] . "</td><td>" . $row[2] . "</td><td>" . $row[3] . "</td><td>" . $row[4] . "</td><td>" . $row[5] . "</td><td>" . $row [6] . "</td><td>" . $row[7] . "</td><td>" . $row[8] . "</td><td>" . $row2 [0] . "</td><td><form method=\"post\"><input name=\"" . $row[0] . "\" type=\"submit\" value=\"" . $row[0] . "\"</form></td></tr>";
}
echo "</table>";
if (isset($_POST[$row[0]])){
$admin_email = "adminmail#mail.com," . $row[2];
mail($admin_email, "Ontvangstbewijs geleverde hardware herverstuurd", "url?id=" . $row [0] , "From:" . "example#email.com");
mysqli_close($conn);
}
?>
Theres a few things I think are worth pointing out firstly.
Table header tags should be in a THEAD tag. See example here.
Try and avoid SQLs within a loop. If possible create a SQL that does everything for you to reduce the number of queries sent to MySQL.
Ok back to your question. Personally, what I would do is have a FORM tag in a TD tag. This way each button can have it's own unique id set in a hidden INPUT tag.
For example:
<tr>
<td>
<form action="[[YOUR-PHP-FILE]]" method="post">
<input type="hidden" name="id" value="[[ID]]" />
<input type="submit" value="Submit" />
</form>
</td>
</tr>
That way you don't need to focus on the submit button but rather the id.
I am using simple code to retrieve data from database table but not getting result it always shows "No Result found".
Table Structure
rollno Varchar(50) Primary Key,
name Varchar(100),
fname Varchar(100),
mname Varchar(100),
course Varchar(100),
duration Varchar(100),
address Varchar(100),
image blob.
HTML FORM CODE
<form name="input" action="q.php" target="display" method="post" >
Roll No: <input type="text" name="name">
<input type="submit" name="submit" value="Submit">
</form>
PHP CODE
<?php
if (isset($_POST['name'])) {
$con=mysqli_connect("mysql.1freehosting.com","u890130056_certi","samsungk2","u890130056_certi");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$name = htmlspecialchars($_POST['rollno']);
{
$result = mysqli_query($con,"SELECT * FROM certificate where rollno ='$name'");
if(mysqli_num_rows($result)>0){
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['rollno'] . "</td>";
echo "<td>" . $row['course'] . "</td>";
echo "<td>" . $row['duration'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['fname'] . "</td>";
echo "<td>" . $row['mname'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>";?><img src="<?php echo $row["image"]; ?> " height="100" width="100"> <?php echo "</td>";
echo "</tr>";
}
}
else
{
echo "<tr><td colspan='4'> No Data Found , Please check your registration no. or contact the institute for clarification. ".$line.'</td></tr>';
}
mysqli_close($con);
}}
?>
name = htmlspecialchars($_POST['rollno']);
where are you getting this 'rollno??
i dont get why are you saving the result by posting['rollno'] because the name of your feild is 'name'.
change this line
$name = htmlspecialchars($_POST['rollno']);
to
$name = htmlspecialchars($_POST['name']);
Here is code I use to update data of one row of form table. This is the code I wanna change.
The change should allows me update not only one row of the shown table but every line:
if(isset($_POST['update'])){
$UpraveneQuery = "UPDATE uctovnictvo SET meno='$_POST[meno]', datum='$_POST[datum]', obchod='$_POST[obchod]', druh='$_POST[druh]', cena='$_POST[cena]', Poznamka='$_POST[poznamka]' WHERE uct_id='$_POST[hidden]' "; //--------------
mysql_query($UpraveneQuery, $con);
};
here are data with which I work for this particular case:
$sql = "SELECT * FROM uctovnictvo WHERE meno IN ('" . implode('\', \'', $option_meno) . "') AND obchod IN ('" . implode('\', \'', $option_obchod) . "') AND druh IN ('" . implode('\', \'', $option_druh) . "') AND datum BETWEEN '$date_start' AND '$date_end' ORDER BY $order";
$mojeData = mysql_query($sql,$con) or die($sql."<br/><br/> Chyba 1 je:".mysql_error());//run query a ulozit to premennej
here is code which generates form table which allows me to change content of it. Every row of the table has its own button for deleting or updating the row.
NOW
When I clik on ulozit zmenu (update) on certain row it allows me to post values of that one row and update variables of the row (query).
PLAN
I dont want to have button ulozit zmenu (update) next to every row of the table but only one butto which will post values of every shown row of the table.
while($zaznam = mysql_fetch_array($mojeData))
{
echo "<form action=index-5.php method=post>";
echo "<tr>";
echo "<td>" . "<input type=text name='meno[]' value='" . $zaznam['meno'] . "'>" . "</td>";
echo "<td>" . "<input type=date name='datum[]' value='" . $zaznam['datum'] . "'>" . "</td>";
echo "<td>" . "<input type=text name='obchod[]' value='" . $zaznam['obchod'] . "'>" . "</td>";
echo "<td>" . "<input type=text name='druh[]' value='" . $zaznam['druh'] . "'>" . "</td>";
echo "<td>" . "<input step=any type='number[]' name=cena value='" . $zaznam['cena'] . "'>" . "</td>";
echo "<td>" . "<input type=text name='poznamka[]' value='" . $zaznam['poznamka'] . "'>" . "</td>";
echo "<td>" . "<input type=submit name='update[]' value='ulozit zmenu'" . ">" . "</td>";[]
echo "<td>" . "<input type=submit name='zmazat[]' value='zmazat'" . ">" . "</td>";
echo "</tr>";
}
I have a problem in editing data in my database. I only want to edit one entry but after I clicked edit, it shows that all the data in my database were edited.
eventlist.php
<?php
$con=mysqli_connect("localhost","root","root","chess");
$result = mysqli_query($con,"select * from events");
//echo "<a href='dashboard.php'>Home</a><br>";
echo "<table border=1 id='hor-minimalist-a' width='100%'>";
echo "<tr align='center'><td><b>Date</b></td><td><b>Event</b></td><td><b>Special Note</b></td><td colspan='2'>Options</td></tr>";
$a=0;
while($row = mysqli_fetch_array($result))
{
if($a%2==0){
echo "<tr bgcolor='#b2d5ff' width='100'>"."<td>" . $row['date'] . "</td> <td>" . $row['event'] . "</td> <td>" . $row['note'] . "</td>"
. "</td><td><a href='editevent.php?id=" . $row['id'] . "'>Edit</a></td><td><a href='deleteevent.php?id=" . $row['id'] . "'>Delete</a></td></tr>";
}
else{
echo "<tr>"."<td>" . $row['date'] . "</td> <td>" . $row['event'] . "</td> <td>" . $row['note'] . "</td>"
. "</td><td><a href='editevent.php?id=" . $row['id'] . "'>Edit</a></td><td><a href='deleteevent.php?id=" . $row['id'] . "'>Delete</a></td></tr>";
}
$a++;
}
echo '</table>';
echo "<center><a href='addevent.php'><button type='submit' class='button'>Add New</button></a></center>";
?>
editevent.php
<?php
while($row = mysqli_fetch_array($result))
{
echo "ID: <input type='text' name='id' value='$row[id]'><br/>";
echo "Date: <input type='text' name='date' value='$row[date]'><br/>";
echo "Event: <textarea type='text' name='event'>".$row['event']."</textarea><br/>";
echo "Note: <input type='text' name='note' value='$row[note]'><br/>";
}
?>
updateevent.php
<?php
$id = $_POST['id'];
$date = $_POST['date'];
$event = $_POST['event'];
$note = $_POST['note'];
$con = mysqli_connect("localhost","root","root","chess");
mysqli_query($con,"update events set date='$date', event='$event', note='$note' where id = id");
header('location: eventlist.php');
?>
"update events set date='$date', event='$event', note='$note' where id = id"
where id = id is true for all rows, so all rows get updated. You probably ment to write where id = $id.
Also note that your query is open to SQL injection. Use prepared statements instead.
Guys I cant see why it is that my code will only Update the last row on the table. It will populate the entire HTML page with a table with the info from phpAdmin. I can then change this info, on the html page. It all works fine if there is only one record, anymore than one and it only takes effect on the last row. I am new to all this, so excuse the code, here it is......
<html>
<?php
$con = mysql_connect("localhost","root");
if(!$con){
die("Cant get there Bren: " . mysql_error());
}
mysql_select_db("Web_Data",$con);
if (isset($_POST['update'])){
$UpdateQuery = "UPDATE Vehicles SET Vehicle_Id='$_POST[Vehicle_Id]',
Registration='$_POST[Registration]',Make='$_POST[Make]',Model='$_POST[Model]',
Classification='$_POST[Classification]',Rental_Price='$_POST[Rental_Price]',
Current_Status='$_POST[Current_Status]',Mileage='$_POST[Mileage]'
WHERE Vehicle_Id='$_POST[hidden]'";
echo "<center> Vechicle Id '$_POST[hidden]' succesfully VEHICLE UPDATED </center>";
mysql_query($UpdateQuery, $con);
};
$sql = "Select * From Vehicles";
$myData = mysql_query($sql,$con);
echo" <center> <table border = 3>
<tr>
<th>Vehicle_Id</th>
<th>Registration</th>
<th>Make</th>
<th>Model</th>
<th>Classification</th>
<th>Rental_Price</th>
<th>Current_Status</th>
<th>Mileage</th>
</tr></center>";
while($record = mysql_fetch_array($myData)){
echo "<form action = UpdateWD.php method=post>";
echo "<tr>";
echo "<td>" . "<input type = text name = Vehicle_Id value=" . $record['Vehicle_Id'] . " </td>";
echo "<td>" . "<input type = text name = Registration value=" . $record['Registration'] . " </td>";
echo "<td>" . "<input type = text name = Make value=" . $record['Make'] . " </td>";
echo "<td>" . "<input type = text name = Model value=" . $record['Model'] . " </td>";
echo "<td>" . "<input type = text name = Classification value=" . $record['Classification'] . " </td>";
echo "<td>" . "<input type = text name = Rental_Price value=". $record['Rental_Price'] . " </td>";
echo "<td>" . "<input type = text name = Current_Status value=" . $record['Current_Status'] . " </td>";
echo "<td>" . "<input type = text name = Mileage value=" . $record['Mileage'] . " </td>";
echo "<td>" . "<input type = hidden name = hidden value=" . $record['Vehicle_Id'] . " </td>";
echo "<td>" . "<input type = submit name = update value= update" . " </td>";
echo "</from>";
}
echo"</table>";
mysql_close($con);
?>
<br><br><br>
<footer>
Copyright © 2013 ABU LTD
About -
Privacy Policy -
Contact Us
Logout
</footer> <!--footer-->
</html>
See here:
Use while loop to display all fetched records.
<?php
// Make a MySQL Connection
$sql = "Select * From Vehicles";
$myData = mysql_query($sql,$con) or die(mysql_error());
while($row = mysql_fetch_array($myData)){
echo $row['Vehicle_Id'];
}
?>
And mysql_query can't use multiple queries.
The easiest thing is to just run them separately. I believe you can do multi query but I haven't tried it.
Just get the idea how you can run multiple queries using foreach loop.
$updateArray = array(21=>300,23=>200,24=>100);
foreach($updateArray as $id=>$value)
{
$query = "UPDATE cart SET cart_qty='$value' WHERE cart_id = '$id'";
mysql_query($query,$link);// $link is specified above
}
here is my mistake
hours and hours and all it was
echo "<td>" . "<input type = 'submit' name = 'update' value= 'update'" . " />";
echo "</td>";
echo "</tr>";
echo "</form>";
had "form" spelt "from" & didnt close the </tr>