heloo i have an ajx call function which brings information from a dropdown populated into a table with text inputs by ajax.
i was wondering if there was anyway that i could update the record in the database by using these text fields and the UPDATE function i am relativity new and the internet didnt bring much to light.
i have a button appearing in this table from a drop down but as far as i am aware you cannot use forms within php and the page this would have been submitted from is already submitting a php function and 2 can not be submitted at once.
i was wondering if it was possible that when the data in the textboxes below is changed when the user clicks the button those details are updated in the database?
im new to ajax and php so help would be amazing.
ps. i know this isnt secure i want it to be functional first and before it goes live i will secure it.
here is the code:
<?php
$q = $_GET['q'];
$con = mysqli_connect('server','uid','pwd','dbname');
if (!$con)
{
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"account.php");
$sql="SELECT * FROM account WHERE name = '".$q."'";
$result = mysqli_query($con,$sql);
echo "<table border='1'>
<tr>
<th>Your Name</th>
<th>Your Email</th>
<th>Your Password</th>
<th>Your User Level</th>
<th>Save Changes</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td> <input type='text' name='txt_yourname' id='txt_yourname' value='" .$row['name']."' required='required' /> </td>";
echo "<td> <input type='text' name='txt_email' id='txt_email' value='" .$row['email']."' required='required' /> </td>";
echo "<td> <input type='text' name='txt_password' id='txt_password' value='" .$row['password']."' required='required' /> </td>";
echo "<td> <input type='text' name='txt_userLevel' id='txt_userLevel' value='" .$row['user_level']."' required='required' /> </td>";
echo "<td> <input type='button' name='btn_user' id='txt_user' type='submit' value='cheese'/> </td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
These statements execute user input, opening you up to a SQL Injection attack. You'll want to not do this.
$q = $_GET['q'];
$sql="SELECT * FROM account WHERE name = '".$q."'";
$result = mysqli_query($con,$sql);
To answer your question, in order to update a row like you want you'll need a way to identify in the database the row that has changed. One way to do it would be to include the row's primary key as a field/attribute in the HTML table row, but I'll leave it to someone more well versed in this area to say whether that's a good idea.
You're also going to want to escape and check the type of all of the fields the user can input when you go to do the update.
Related
I'm making a functionality which shows shops in a database in rows with a button behind each shop, and a checkbox in front of each shop. I would like to find another way of getting the id of the shop based on the button pressed. The shop of which the button is selected gets deleted from the database.
$sql = "SELECT * FROM alb_locaties WHERE Verwijderd = '0'";
$result = $conn->query($sql);
if ($result->num_rows > 0){
echo "<form action='' method='get'>";
echo "<table id='MainTable'>
<tr>
<th></th>
<th>Shopnaam</th>
<th></th>
</tr>";
while ($row = $result->fetch_assoc()){
$ID = $row['I_id'];
echo "<tr>
<td><input type='checkbox' name='Shops[]' value='$ID'></td>
<td>$ID</td>
<input type='hidden' name='givenID' value='$ID'>
<td><input type='button' value='Verwijder'/></td>
</tr>";
}
echo "</table>";
echo "<input id='SelectionButton' type='submit' name='SelectionDelete' value='Verwijder selectie'/>";
echo "</form>";
}
if (isset($_GET['givenID'])){
$selectedID = $_GET['givenID'];
// FIRST DELETE
$sql = "SELECT * FROM alb_locaties INNER JOIN alb_afdelingen ON alb_locaties.I_id = alb_afdelingen.locaties WHERE alb_locaties.I_id = {$selectedID}";
Now it's determined by <a href=\"Reset_locatieshops.php?givenID='$ID'\"> and $selectedID = $_GET['givenID'];, but I would rather use a way in which it doesnt show the ID in the URL. Post also seems like a better option, because now when I refresh the page it executes the query again because of the ID in the URL. How can I still get the ID, but by using a different method.
Nevermind I found it. I changed the form method to post,
I changed the <input type='button' value='Verwijder'/> to <input type='submit' value='Verwijder' name='submit'>.
After that I used $selectedID = $_POST['givenID']; to get the ID given to the <input type='hidden' name='givenID'
I am working on a project where I want users to choose their space for parking and book it. I have a MySQL database which holds information about parking slots. I am fetching those values and display it using table and form. I have put checkboxes to make choice. and once they make choice they should be directed to payment page.
I am having problem with checkbox. I can see that in value fields it has values from database but when I hit submit button it doesn't pass any values to next page.
below is my code
<body>
<form method="POST" action="book.php">
<?php
//we create a table
echo "<table>";
// create table th
echo "<tr > <th> Parking Slot No </th> <th> Status </th>";
$sql=" select ParkingSlotNo,Status from fleming_dwing ";
$st=$conn->prepare($sql);
$st->execute();
$total=$st->rowCount();//get the number of rows returned
if($total < 1 ){//if no row was returned
echo "<tr> <td style> No Data: DataBase Empty </td> ";//print out error message
echo "<td> No Data: DataBase Empty </td> ";//print out error message
$ing = "<img src='img/occupied.png'/>" ;
}
else{
while($res = $st->fetchObject()){//loop through the returned rows
echo "<tr>";
if($res->ParkingSlotNo and $res->Status=='OCCUPIED')
{echo "<td> $res->ParkingSlotNo </td> ";
echo "<td> <img src='img/occupied.png'/> </td>";
echo"<td><a href=''> </a> </td>";
}
else
{
echo "<td> $res->ParkingSlotNo </td> ";
echo "<td> <img src='img/vacant.png'> </td>";
echo"<td><input type='checkbox' value='$res->ParkingSlotNo'></td>";
}
echo"</tr>";
}
}
?>
</table>
<input type="submit" value="Submit">
</form>
</body>
</html>
and this is the code for booking page
<?php
require_once("dbconfigpdo.php");
print_r($_POST);
?>
The checkboxes do not have name attributes. A form control can't be successful (included in the name=value pairs of data that are submitted) without one.
Any input must have a name attribute.
By name you can use a value. So, you need to add name="your name" to your checkboxes.
You need to set an attribute name to your input field, or it will not be processed.
Something like:
<input name='parkingslot' type='checkbox' value='$res->ParkingSlotNo'>
I want add a checkbox in every row of a table in php.
How can I fill a database cell with Yes or No by CheckBox in PHP?
When I change checkbox name for example "name", I have an error.
<?php
$con=mysql_connect("localhost","biterium_login","login");
mysql_select_db("biterium_login");
// Check connection
if (!$con){
die("به دلیل مشکل زیر، اتصال برقرار نشد : <br />" . mysql_error());
}
$result = mysql_query("SELECT * FROM member",$con);
echo "<table border='1'>
<tr>
<th>id</th>
<th>name</th>
<th>phone</th>
<th>email</th>
<th>action</th>
<th>action</th>
<th>active</th>
</tr>";
while($row = mysql_fetch_assoc($result))
{
echo "<tr>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['phone']."</td>";
echo "<td>".$row['email']."</td>";
echo "<td><a href='delete.php?id=".$row['id']."'>Delete</a></td>";
echo "<td><a href='FormUpdate.php?id=".$row['id']."'>Edit</a></td>";
echo "<td><input type='checkbox' value='".$row['id']."' name='".$row['id']."'></td>";
echo "</tr>";
}
echo "</table>";
?>
<a href='add.html'>ADD</a></td>
There are a couple of ways to do it. You can either use a hidden form element like this:
echo "<td><input type='hidden' name='status' value='N'><input type='checkbox' name='status' value='Y'></td>";
(I renamed the element to be 'status' rather than the row's id to make it more generic.)
This method takes advantage of the fact that the unchecked element will not be sent when submitting the form but the hidden element will be. If the checkbox is selected, the value from the checkbox will override the value of the hidden element because the checkbox element comes after the hidden element in the form.
Or you can check if the status field exists in the submitted form data and set 'Y' or 'N' accordingly.
This is all moot anyway because your code doesn't submit forms or process data anywhere that you've shown us.
Hey Guys, I have a question for you.
Imagine that I wanted to be able to keep track of how many miles I've ran every week, so that I could
compare it to the goals I've set for each week. So i've created this table by the use of mysql_fetch_row.
$result=mysql_query("SELECT * FROM randomtable ORDER BY week ASC");
echo "<Table id='result' cellspacing='0'>
<tr class='toprow'>
<th>Week</th>
<th>Goal</th>
<th>Actual Number of Miles</th>
</tr>";
while($row = mysql_fetch_row($result))
{
echo "<tr class='standardrow'>";
echo "<td>$row[0]</td>";
echo "<td>$row[1]</td>";
echo "<td><form><input method='post' type='number'></form></td>";
echo "</tr>";
}
echo "</table>";
This piece of code resultet in a table with 10 weeks with 10 goals - and a column for the actual number of miles. This column should include 10 input forms where the actual number of miles can be submitted. But how do I relate the input from the submit form to the row in which the submit form is positioned?
The primary key is the week - so this would be the one to relate to.
Hope you understand what my problem is:)
To do this you would use a hidden input field.
When you echo each row, and the form in that row, you would simply add an extra line:
`<input type="hidden" name="row_id" value="' . $row['id_column'] . '" />';
In full, your code would be:
$result=mysql_query("SELECT * FROM randomtable ORDER BY week ASC");
echo "<Table id='result' cellspacing='0'>
<tr class='toprow'>
<th>Week</th>
<th>Goal</th>
<th>Actual Number of Miles</th>
</tr>";
while($row = mysql_fetch_row($result))
{
echo "<tr class='standardrow'>";
echo "<td>$row[0]</td>";
echo "<td>$row[1]</td>";
echo "<td>
<form>
<input method='post' type='number'>
<input type='hidden' name='row_id' value='" . $row['id_column'] . "' />
</form>
</td>";
echo "</tr>";
}
echo "</table>";
I think there should be some modifications that has to be done in loop.
echo "<td><form method='post'><input type='number' value='".$rows['col_name']."'><input type='submit' ></form></td>";
This code adds a submit button to each row. But, this shouldn't be what I think.
It should be rather this way,
echo "<form method='post'> ";
while($row = mysql_fetch_row($result))
{
echo "<tr class='standardrow'>";
echo "<td>$row[0]</td>";
echo "<td>$row[1]</td>";
echo "<td><input type='number' value='".$rows['col_name']."'></td>";
echo "</tr>";
}
echo "<input type='submit' ></form>";
Or make the input field look like this.
'<input type="text" name="row['.$row['id_column'].'][miles]" />';
It will return you an array when you post it.
foreach($_POST['row'] as $key => $value){
// $key is your primary key
// $value['miles'] is your input value
}
So basically I don't really know how to explain what i need or if the title is even correct but i need some help.I'll explain what I'm trying to do the best I can.
This is a picture of the page I'm working on and referring to
http://i40.tinypic.com/14m5euh.png .
SO basically what I need to do is,when the user uploads a file and clicks on upload it will upload to a database along with the job number,so far I've managed to get it to update into the database.
But I can't get the job number in the database because my information is all in an array from the database I tried to store the information into a session variable,its loops and gets replaced by the next one,so basically what ever the last job number is will be stored in the database which of cause is wrong?
The last problem I've is,how do I get the users answer from the drop down box into a variable ?
My code:
$con = mysqli_connect("localhost", "root", "","fixandrun") or die(mysqli_error());
$data = mysqli_query($con,"SELECT * FROM bookjob") or die(mysql_error());
Print "<table border cellpadding=10>";
Print "<table border='2'>";
echo "<table border='2' cellpadding=10>
<tr>
<th> Job number</th>
<th> Job details</th>
<th> Pc number </th>
<th> Job status</th>
<th> Upload report</th>
<th> Update Job</th>
</tr>";
while($row= mysqli_fetch_array($data))
{
echo "<tr>";
echo "<td>".$_SESSION['JOBNUM'] = $row['jobnumber'] . "</td>";
echo "<TD width=20% height=100>" . $row['jobdetails'] . "</td>";
echo "<td>" . $row['pcnumber'] . "</td>";
echo "<td> <select name='jobprogress'>
<option Value='pending'>pending</option>
<option value='Completed'>Completed</option>
<option value='In progress'>In progress</option>
<option value='Need more information'>Need more information</option>
</select> </td>";
echo "<td> <form action='reportupload.php' method='post' enctype='multipart/form-data' name='uploadform'>
<input type='hidden' name='MAX_FILE_SIZE' value='350000'>
<input name='report' type='file' id='reportupload' size='50'>
<input name='upload' type='submit' id='upload' value='Upload'>
</form>
</td>";
echo "<td> <a href='updateadmin.php'>Update information</a></td>";
echo "</tr>";
}
echo "</table>";
echo "<br>";
There are many problems with your code. It would be extremely time consuming to go through them all, so I'll run down some things you need to look at to get this going.
FIrstly, you can make as many forms as you want, and with the help of PHP you can do this dynamically as youre doing. The problem is for every job, youre creating a new form with the same exact name. You need to find a way to make them the name of each form dynamic if youre going to do that.
Secondly, you probably shouldnt make a new form, so take all your opening and closing tags out of the loop, put the opening form tag before the while, and the ending form tag after the while loop.
I would suggest you go read about HTML forms a little more and PHP while loops and fetching information from a database with embedded forms before messing with this more. No offence, but I think you need a little more knowledge on this before fiddling with it.
I would suggest making a test database and table with maybe 3 fields, then making a small form with a couple field and seeing if you can fetch the data and update it with your form first.
i think i have done what you ment, is this right now?
$cnt = 0;
while($row= mysqli_fetch_array($data))
{
echo " <form action='test.php' method='post' enctype='multipart/form-data' name='uploadform' . $cnt>";
echo "<tr>";
echo "<td>".$_SESSION['JOBNUM'. $cnt] = $row['jobnumber'] . "</td>";
echo "<TD width=20% height=100>" . $row['jobdetails'] . "</td>";
echo "<td>" . $row['pcnumber'] . "</td>";
echo "<td> <select name='jobprogress'>
<option Value='pending'>pending</option>
<option value='Completed'>Completed</option>
<option value='In progress'>In progress</option>
<option value='Need more information'>Need more information</option>
</select> </td>";
echo "<td>
<input type='hidden' name='MAX_FILE_SIZE' value='350000'>
<input name='report' type='file' id='reportupload' size='50'>
</td>";
echo "<td><input name='upload' type='submit' id='upload' value='Upload'></td>";
echo "</tr>";
echo "</form>";
++$cnt;
}
echo "</table>";
echo "<br>";
?>