Edit SQL with dropdown - php

So just like always, I'm working on code that up until now I have had no experience with.
What I'm trying to do is create a ticket type system for a department here at work to use to contact potential students. I'm ok with the HTML and PHP for the most part, it's learning the SQL that has me stumped. I'm really not even sure if it's all being done right. I may need to incorporate AJAX as well. Basically, the contact will fill out a form with information about what type of degree they want to pursue, and contact info etc... Everything inserts fine into the SQL but the problem comes when I want to edit it from my viewtable.php page.
This is just the working prototype, eventually it will have user login and other such things, as well as admin console, etc... so without further ado, here is my viewtable code. If it's insufficient, please let me know. I'll keep researching but any help you guys can offer would be appreciated.
<?php
session_start();
//Must be called before any code or white space is executed
/*
TODO:
1: Get checkboxes to load and display only what was posted via checkboxes from a previously loaded page
2: Cleanup
3: Hover messages for table headers
4: Program search bar
5: Get dropdown to do something
6: Program to only show 10 entries at a time, and be able to move to the next ten entries
7: program to be able to sort by "Last Name" , "First Name", etc...
8: Move database info to a called file for security and ease of access.
*/
$action=$_POST['action'];
$id=array($_POST['id']);
//Create Connection
$con = mysqli_connect("localhost","username","password","database");
//Check Connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else {
//echo "Connection Successful! \n";
}
//If the action dropdown was set
if($action = 'delete') {
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql ="DELETE FROM persons WHERE PID='$del_id'";
$result = mysql_query($sql);}
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_multiple.php\">";}
}
$query = "Select * FROM persons";
$result = mysqli_query($con, $query);
$num=mysqli_num_rows($result);
echo $id;
echo $action;
?>
<!DOCTYPE html>
<html>
<head>
<style>
tr:nth-child(even) {
background-color:#00fff0;
}
</style>
<script>
</script>
</head>
<body>
<table id="persons" name="persons" border="0" cellpadding="1">
<form name="database" action="viewtable.php" type="post">
<div type="hidden" class="pidmessage" style="display:none;">Unique Identifier</div>
<div type="hidden" class="fnamemessage" style="display:none;">Contact's First Name</div>
<div type="hidden" class="lnamemessage" style="display:none;">Contact's Last Name</div>
<div type="hidden" class="emailmessage" style="display:none;">Contact's Email Address</div>
<div type="hidden" class="phonemessage" style="display:none;">Contact's Home Phone Number</div>
<div type="hidden" class="cellmessage" style="display:none;">Contact's Mobile Phone Number</div>
<div type="hidden" class="campusmessage" style="display:none;">Is the contact interested in residential or online courses?</div>
<div type="hidden" class="statemessage" style="display:none;">Contact's Home State</div>
<div type="hidden" class="studenttypemessage" style="display:none;">The contact is interested in enrolling as this</div>
<div type="hidden" class="termmessage" style="display:none;">The contact is interested in enrolling beginning this term</div>
<div type="hidden" class="enrollmentmessage" style="display:none;">The contact is interested in enrolling in this many credit hours</div>
<div type="hidden" class="degreemessage" style="display:none;">The contact is interested in pursuing this type of degree</div>
<div type="hidden" class="messagemessage" style="display:none;">The contact heard about TTU in this manner</div>
<div type="hidden" class="commentsmessage" style="display:none;">These are comments the contact left</div>
<?php
$i = 0;
$data = array();
while($row = mysqli_fetch_assoc($result)) {
$data[] = $row;
}
$colNames = array_keys(reset($data));
?>
<tr><th> </th>
<?php
foreach($colNames as $colName)
{
echo"<th class='{$colName}'>$colName</th>";
}
?>
</tr>
<?php
foreach($data as $row)
{
echo "<tr>";
echo "<td><input type='checkbox' name='checkbox[]' value='{$row['PID']}'></input></td>";
foreach($colNames as $colName)
{
echo "<td>".$row[$colName]."</td>";
}
echo"</tr>";
}
?>
<tr class="clear"><td> </td><td colspan="7">
<select name="action">
<option value="">--Select One--</option>
<option value="delete">Delete Entry</option>
<option value="assign">Assign</option>
<option value="contacted">Move to contacted</option>
<option value="edit">Edit</option>
<option value="">Cancel</option>
</select></td>
<td colspan="7">
<input type="submit" name="submit" value="Submit"> </input></td></tr>
</form>
</table>
</body>
</html>
<?php
mysqli_close($con);
?>
Note: The todo list that is commented out on top is on the list of things (clearly) todo...

So far from what I see its this section:
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql ="DELETE FROM persons WHERE PID='$del_id'";
$result = mysql_query($sql);}
I don't see where $count is declared, nor $checkbox. If $checkbox is an array, where are you getting it from?
Otherwise, making this work from ajax shouldn't be to hard as long as you're familiar with jQuery. Just cut the code that does the updating to its own php file, and have jQuery pass the form values through ajax to the new script.

Related

use drop down menu to query mysql

Beginner here; I have searched many examples, still need some help.
I would like for the user to select an option from a dropdown box, and that option will query a table in mysql. I don't understand how to (in my html file) show the select statment in getprojectstatus.php.
<html>
<head>
<title>Status Dashboard</title>
</head>
<body style="background:#19245e url('images/fade.png')repeat-x;font-size:12px;font-family: Arial;line-height:18px;color:#FFFFFF;">
<script>
function displayProject(option)
{
var x;
if (option=='sciplay')
{
x="show Sciplay selected, show notes, status, etc..."
}
else if (option=='oklahoma')
{
x="show OK selected, show..."
}
else if (option=='northdakota')
{
x="show North Dakota selected, show..."
}
else if (option=='audit')
{
x="show Audit selected, show..."
}
else if (option=='sggaming')
{
x="show SG Gaming selected, show..."
}
else if (option=='all')
{
x="..."//(option=(1+2+3+4+5))
}
document.getElementById("demo").innerHTML=x;
}
</script>
<div align="center">
<TABLE BORDER="1">
<TR>
<TD><img src='images/header.png'/>
</TD>
</TR>
<TR>
<TD>
<TABLE BORDER="0" bgcolor="C0C0C0" align="left">
<TH>Projects
</TH>
<TR>
<TD>
<FORM action="getprojectstatus.php" method="post">
<SELECT onload="displayProject(this.value);" onchange="displayProject(this.value);">
<OPTION VALUE='all'>ALL</OPTION>
<OPTION VALUE='sciplay'>Sciplay</OPTION>
<OPTION VALUE='oklahoma'>Oklahoma</OPTION>
<OPTION VALUE='northdakota'>North Dakota</OPTION>
<OPTION VALUE='audit'>Audit SSAE16</OPTION>
<OPTION VALUE='sggaming'>SG Gaming</OPTION>
</SELECT>
</FORM>
</TD>
</TR>
</TABLE>
</TD>
</TR>
<TR>
<TD>
<p id="demo">
</p>
</TD>
</TR>
</TABLE>
</div>
</body>
and my getprojectstatus.php file:
<?php
//create connection variables
$host=`localhost`;
$user=`dashboard`;
$pw=`password`;
$db=`status`;
//create connection
$con=mysqli_connect("$host", "$user", "$pw", "$db");
//check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//take input from form and store as var queries
$option= $_POST['option'];
if ($option == 'sciplay')
{
$queries = "SELECT * FROM status where project=1"
}
else if ($option == 'oklahoma')
{
$queries = "SELECT * FROM status where project=2"
}
else if ($option == 'northdakota')
{
$queries = "SELECT * FROM status where project=3"
}
else if ($option == 'audit')
{
$queries = "SELECT * FROM status where project=4"
}
else if ($option == 'sggaming')
{
$queries = "SELECT * FROM status where project=5"
}
else ($option == 'all')
{
$queries = "SELECT * FROM status"
}
//store query as var result
$queries=$query;
$result=#mysqli_query($con,"$query");
//echo var result in table format
echo "<table border='1'>
<tr>
<th>Project</th>
<th>Subject</th>
<th>Status</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['project'] . "</td>";
echo "<td>" . $row['subject'] . "</td>";
echo "<td>" . $row['entry'] . "</td>";
echo "</tr>";
}
echo "</table>";
//close mysql connection
mysqli_close($con);
?>
Thanks for any help in advance!
OK, so it looks like you haven't fully grasped the client/server model employed in web based systems (we were all there at the beginning).
Here's a quick rundown, and then we'll talk about where you're issue lies.
You open a browser and type in a URL, hit enter
Your browser sends a request to the server that hosts the URL you entered
The server parses the request and loads up the specific file that was requested. For our purposes, that means running the PHP file to ultimately generate an HTML and javascript output
It sends this HTML (and javascript) to the browser. No executable PHP is ever sent to the browser, it all stays on the server.
Your browser receives this HTML (and javascript) and displays it.
Your browser can execute any javascript on the page, but if it needs information that is not already in your page source, it has to request it from the server. It can do this one of two ways: a) you can send a whole new request to the server, with the correct parameters such that the information you want is included up front or b) you can run an ajax request to pull the information you need, and then insert it into your page
So, at first glance it looks like the breakdown is occurring between step 5 and 6, you've loaded up a whole page in the browser, and it needs more information from the server but you're never actually sending that request to the server. If you were to send that request to the server (by either adding a submit button to your form, or adding a form submit request to your displayProject() function), then you've got no way (currently) to insert that new information into your existing page.
So, there's two potential answers to your problem:
If you want to understand better how to write a PHP application because you intend to do more of this in the future:
... then you should rewrite these two separate files as one single file. (I've dropped your code in pretty much as-is, I haven't done any checking for errors)
<?php
//create connection variables
$host=`localhost`;
$user=`dashboard`;
$pw=`password`;
$db=`status`;
//create connection
$con=mysqli_connect("$host", "$user", "$pw", "$db");
//check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//take input from form and store as var queries
$option= $_POST['option'];
if ($option == 'sciplay')
{
$queries = "SELECT * FROM status where project=1"
}
else if ($option == 'oklahoma')
{
$queries = "SELECT * FROM status where project=2"
}
else if ($option == 'northdakota')
{
$queries = "SELECT * FROM status where project=3"
}
else if ($option == 'audit')
{
$queries = "SELECT * FROM status where project=4"
}
else if ($option == 'sggaming')
{
$queries = "SELECT * FROM status where project=5"
}
else ($option == 'all')
{
$queries = "SELECT * FROM status"
}
//store query as var result
$queries=$query;
$result=#mysqli_query($con,"$query");
//The actual echo of the table display was moved to the area of the page it actually needs to go
//close mysql connection
mysqli_close($con);
?>
<html>
<head>
<title>Status Dashboard</title>
</head>
<body style="background:#19245e url('images/fade.png')repeat-x;font-size:12px;font-family: Arial;line-height:18px;color:#FFFFFF;">
<script>
function displayProject(option)
{
var x;
if (option=='sciplay')
{
x="show Sciplay selected, show notes, status, etc..."
}
else if (option=='oklahoma')
{
x="show OK selected, show..."
}
else if (option=='northdakota')
{
x="show North Dakota selected, show..."
}
else if (option=='audit')
{
x="show Audit selected, show..."
}
else if (option=='sggaming')
{
x="show SG Gaming selected, show..."
}
else if (option=='all')
{
x="..."//(option=(1+2+3+4+5))
}
document.getElementById("demo").innerHTML=x;
}
</script>
<div align="center">
<TABLE BORDER="1">
<TR>
<TD><img src='images/header.png'/>
</TD>
</TR>
<TR>
<TD>
<TABLE BORDER="0" bgcolor="C0C0C0" align="left">
<TH>Projects
</TH>
<TR>
<TD>
<!--
SOME CHANGES HERE: The form action is now pointing
to this same file, so that when you submit your form,
it goes back to this same file.
Also, I gave your select tag a "name" attribute so
that when you submit it, it'll actually be accessible
in the $_POST['option'] variable
-->
<FORM action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<SELECT name="option" onload="displayProject(this.value);" onchange="displayProject(this.value);">
<OPTION VALUE='all'>ALL</OPTION>
<OPTION VALUE='sciplay'>Sciplay</OPTION>
<OPTION VALUE='oklahoma'>Oklahoma</OPTION>
<OPTION VALUE='northdakota'>North Dakota</OPTION>
<OPTION VALUE='audit'>Audit SSAE16</OPTION>
<OPTION VALUE='sggaming'>SG Gaming</OPTION>
</SELECT>
<input type="submit" value="Reload Form">
</FORM>
</TD>
</TR>
</TABLE>
</TD>
</TR>
<TR>
<TD>
<!-- a <p> tag can't technically hold a <table> tag, but one thing at a time here -->
<p id="demo">
<?php
// I'm just lifting your structure out and placing it here for clarity purposes.
// really, you can just write these tags out in HTML directly,
// not go into PHP and echo them
echo "<table border='1'>
<tr>
<th>Project</th>
<th>Subject</th>
<th>Status</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['project'] . "</td>";
echo "<td>" . $row['subject'] . "</td>";
echo "<td>" . $row['entry'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</p>
</TD>
</TR>
</TABLE>
</div>
</body>
... so, what's going on here is that I've changed it up so that the data is being built and used, all in the same file. Your page now doesn't need to request new information, it's all right there, and if you want to change the form, then you submit and it requests the whole page again, now with the new information.
That's all well and good, but may not be exactly how you want your page to work. That said, you should understand how that process functions because any PHP application of even basic complexity will need to work in that way at least in part.
Now...
If you just want this page to function the way it appears you -want- it to function, i.e. you build your page, then request new information to update your page on form request:
... then we can get that done. Again, I haven't done any checking for errors. First things first, though, put this in your <head> tags:
<head>
<title>Status Dashboard</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
</head>
... we're going to be using jquery, just because in our case here, it let's us just worry about running the ajax request rather than bother setting up all of the necessary ins and outs. Then you want to add this to your displayProject function (just before the closing } ):
$.ajax({
url: 'getprojectstatus.php',
type: 'post',
data: 'option='+option,
success: function(data) {
$('#demo').html(data);
}
});
... all that's going on there is that you're sending your "option" selection to getprojectstatus.php, that file is generating your HTML output for your table, and then once it's received, it runs "success", which just inserts the data directly into the element with the "demo" id attribute. You should consider changing that to a <div>, at minimum, as the <p> might cause headaches.
I am not sure if I understand the problem correctly, but this might work for you:
<FORM action="getprojectstatus.php" method="post">
<SELECT onload="displayProject(this.value);" onchange="displayProject(this.value);">
<OPTION VALUE='none'>ALL</OPTION>
<OPTION VALUE='1'>Sciplay</OPTION>
<OPTION VALUE='2'>Oklahoma</OPTION>
<OPTION VALUE='3'>North Dakota</OPTION>
<OPTION VALUE='4'>Audit SSAE16</OPTION>
<OPTION VALUE='5'>SG Gaming</OPTION>
</SELECT>
</FORM>
And then in your PHP file:
$option= $_POST['option'];
$queries = "SELECT * FROM status"
if ($option != 'none'){
$queries = "SELECT * FROM status where project=".$option
}
I would also recommend researching some PHP Frameworks, maybe CodeIgniter and learning MVC patterns.

Why is my php form not generating values?

How do you create a form in PHP where the results entered in from the user will be generated in a table? When I test the pages, I only get the start of the table, key and results(values) fields but no values!
Here is my code (I'm shortening the number of fields to two since forms can be very long:
Order form:
<?php
foreach ($_POST as $key => $entry)
{
$totalfields++;
echo "<tr>\n";
echo "<td>" . $key . "</td>";
if (is_array ($entry)) {
$count = count($entry);
echo "<td>";
for ($i=0; $i<$count; $i++){
echo $entry[$i] . "<br />";
}
echo "</td>";
} else
{
echo "<td>$entry</td>";
}
echo "<tr>\n";
}
?>
<form method="post" action="address to the results form page">
<label for="fname">First Name:</label>
<input type="text" name="fname" id="fname"/><br/>
<br/>
<label for="lname">Last Name:</label>
<input type="text" name="lname" id="lname"/><br/>
<br/>
<p><input type="submit" value="Submit"/></p>
</form>
Results form page:
<div id="results table">
<table border='1'>
<tr>
<th>
Field
</th>
<th>
Results
</th>
</tr>
</table>
</div>
This actually posts fine for me (I used PHP_SELF for the form action). I would however suggest that you use $_SERVER[REQUEST_METHOD] to check for POST:
if( $_SERVER[REQUEST_METHOD] == 'POST' ) { ...
In addition, be explicit about calling the posted variables ($_POST[fname] & $_POST[lnane])
Your PHP generating the table should be in the resulting page, not in the form page. Or else, you can point the action of the form to the same page, to get the results posted and processed on the same page.
EDIT: What I mean is this: PHP — as you may know — is processed server-side. It means that your browser sends a request to the server, the server processes the PHP file and then it serve the client the processed version.
The problem in your code is that you have the PHP script processing the $_POST on the page in which the user is supposed to fill up the form. When a user fills up the form and hit submit, he will get directed to the action page, where the $_POST will be available and processed. Your misunderstanding is in the fact that you expect the $_POST to be available on the page where it's posted, and then to send it automagically to the result page. This is not what happens because the server doesn't have an opportunity to catch the information you sent on the same page, because it has already been served…
…unless you make the form action direct to the same page. Then the server catches the information and processes it.
You could go solving this problems in two ways really.
Point your action to the result page as you are doing now, but move your processing code to the resulting page.
Point your action to the same page.
You may even want to do something fancy, like check if there is $_POST information and, if there isn't, show a form. If there is, show the result. This is not at all difficult to implement as it follows:
<?php
if( $_POST ): ?>
<div id="results table">
<table border='1'>
<thead>
<tr>
<th>
Field
</th>
<th>
Results
</th>
</tr>
</thead>
<tbody>
<?php
foreach ($_POST as $key => $entry)
{
echo "<tr>\n";
echo "<td>" . $key . "</td>";
if (is_array ($entry)) {
$count = count($entry);
echo "<td>";
for ($i=0; $i<$count; $i++){
echo $entry[$i] . "<br />";
}
echo "</td>";
} else
{
echo "<td>$entry</td>";
}
echo "<tr>\n";
}
?>
</tbody>
</table>
</div>
<?php else: ?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label for="fname">First Name:</label>
<input type="text" name="fname" id="fname"/><br/>
<br/>
<label for="lname">Last Name:</label>
<input type="text" name="lname" id="lname"/><br/>
<br/>
<p><input type="submit" value="Submit"/></p>
</form>
<?php endif; ?>
I made a working example for you. Just go to the link and click the Run [F9] button over the top to see it in action:
Working example

populating text fields from the sql using dropdown list Jquery

Hello there first time doing this, Basically I am rather confused on how to Re-populate text boxes from the database.
My current issue is that basically I have two tables in my database 'USER' and 'STATISTICS'.
Currently what is working is that my code is looking up the values of 'User_ID' in the 'USER' table and populating the values in the drop down list.
What I want from there is for the text fields to populate corresponding to those values from the database looking up the 'User_ID' E.G 'goal_scored' , 'assist', 'clean_sheets' and etc.
I am pretty baffled I have looked up on various different questions but cannot find what im looking for.
<?php
$link = mysql_connect("localhost","root","");
mysql_select_db("f_club",$link);
$sql = "SELECT * FROM user ";
$aResult = mysql_query($sql);
?>
<html>
<body>
<title>forms</title>
<link rel="stylesheet" type="text/css" href="css/global.css" />
</head>
<body>
<div id="container">
<form action="update.php" method="post">
<h1>Enter User Details</h1>
<h2>
<p> <label for="User_ID"> User ID: </label> <select id="User_ID" id="User_ID" name="User_ID" >
<br> <option value="">Select</option></br>
<?php
$sid1 = $_REQUEST['User_ID'];
while($rows=mysql_fetch_array($aResult,MYSQL_ASSOC))
{
$User_ID = $rows['User_ID'];
if($sid1 == $id)
{
$chkselect = 'selected';
}
else
{
$chkselect ='';
}
?>
<option value="<?php echo $id;?>"<?php echo $chkselect;?>>
<?php echo $User_ID;?></option>
<?php }
?>
I had to put this in because everytime I have text field under the User_ID it goes next to it and cuts it off :S
<p><label for="null"> null: </label><input type="text" name="null" /></p>
<p><label for="goal_scored">Goal Scored: </label><input type="text" name="Goal_Scored" /></p>
<p><label for="assist">assist: </label><input type="text" name="assist" /></p>
<p><label for="clean_sheets">clean sheets: </label><input type="text" name="clean_sheets" /></p>
<p><label for="yellow_card">yellow card: </label><input type="text" name="yellow_card" /></p>
<p><label for="red_card">red card: </label><input type="text" name="red_card" /></p>
<p><input type="submit" name="submit" value="Update" /></p></h2>
</form>
</div>
</body>
</html>
If anyone can help with understanding how to get to the next stage would be much appreciated thanks x
Rather than spending time on something complicated like AJAX, I'd recommend going the simple route of pages with queries, such as user.php?id=1.
Craft a user.php file (like yours) and if id is set (if isset($_GET['id'])) select that user from the database (after having sanitised your input, of course) with select * from users where id = $id (I of course assume you have an id for each user).
You can still have the <select>, but remember to close it with </select>. You might end up with something like this:
<form method="get">
<label for="user">Select user:</label>
<select name="id" id="user">
<option value="1">User 1</option>
...
</select>
<submit name="submit" value="Select user" />
</form>
This will send ?id=<id> to the current page and you can then fill in your form. If you further want to edit that data, create a new form with the data filled in with code like <input type="text" name="goal_scored" value="<?php echo $result['goal_scored']; ?>" /> then make sure the method="post" and listen on isset($_POST['submit']) and update your database.
An example:
<?php
// init
// Use mysqli_ instead, mysql_ is deprecated
$result = mysqli_query($link, "SELECT id, name FROM users");
// Create our select
while ( $row = mysqli_fetch_array($link, $result, MYSQL_ASSOC) ) {?>
<option value="<?php echo $result['id']; ?>"><?php echo $result['name'] ?></option>
<?php}
// More code ommitted
if (isset($_GET['id'])) {
$id = sanitise($_GET['id']); // I recommend creating a function for this,
// but if only you are going to use it, maybe
// don't bother.
$result = mysqli_query($link, "SELECT * FROM users WHERE id = $id");
// now create our form.
if (isset($_POST['submit'])) {
// data to be updated
$data = sanitise($_POST['data']);
// ...
mysqli_query($link, "UPDATE users SET data = $data, ... WHERE id = $id");
// To avoid the 'refresh to send data thing', you might want to do a
// location header trick
header('Location: user.php?id='.$id);
}
}
Remember, this is just an example of the idea I'm talking about, lots of code have been omitted. I don't usually like writing actually HTML outside <?php ?> tags, but it can work, I guess. Especially for smaller things.

Display result in dropdown and select to perform query in php mysql

I have a database named Data which has a table in which their are different names of products their id and prices, i want to make a web page using php so that i can edit,add and save the items from the web page to the DB and search the names accordingly.
<html>
<head>
<title>Products store</title>
</head>
<body>
<p style="font-size:20px" align="center"> <b>Product Database Editor</b> </p>
<p>
<form method="post">
Enter Product Name: <input type="text" name="pname" id="pname" size="70">
<input type="submit">
</p>
<form method="post">
<select id="opt" name="opt">
<?php
$pname = $_REQUEST['pname'];
// $pname= mysql_real_escape_string($_POST['pname']);
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("Dataentry", $con);
$result = mysql_query("SELECT * FROM products where name like '%$pname%'");
$result_rows = mysql_num_rows($result);
if($pname==NULL)
{
echo "Please enter a product name!";
}
else if($result_rows==0)
{
echo "Product Name does not exist!";
}
else
{
while($row = mysql_fetch_array($result))
{
$name = $row['name'];
echo "<option value='$name_selected'>$name</option>";
//echo ("<option value = '" . $row['name'] . "'>" . $row['id'] . "</option>");
echo $name_selected;
echo "<br />";
}
}
mysql_close($con);
?>
</select>
</form>
</body>
</html>
when i run this code i get the names list in the dropdown but after i select any name, nothing happens, how should i modify my code so that i can select any name from the dropdown and then be able to fetch the price of that particular name to edit it.
please help, coding will be much helpful.
Suppose you have a question like this in your dropdown menu.
Q - How many colors does the US flag has?
Now, from what I understand you want your choice from the drop down menu to appear instantly..
Well, here is a simple select form.
<form method="post">
<select id="opt" name="opt">
<option value="four">four</option>
<option value="five">five</option>
<option value="two">two</option>
<option value="million">million</option>
</select>​
And, the JS code:
$(document).ready(function() {
$("#opt").change(function() {
alert($(this).val());
});
});​
Now, click her a DEMO with jsFiddle to show you, how it works.
You can copy/paste the codes and include them in your site, this is a simple code, but you if you small knowledge of Javascript you can manipulate the data the way you need it to appear. .
To get a value of an input (this case, from a dropdown) on the fly, you need to use client-side scripting language javascript (or jquery) and use ajax to sent it to server-side, where the code is in PHP.

UPDATE inside a WHILE statement

So, I have a page with a bunch of workorders on it. Each workorder is a row in a single table, and gets put on the page with a while() statement.
I'm trying to update each row with a simple form that I put inside the while(), and an UPDATE/WHERE statement to actually add the information to the table.
Instead of adding it to the specific row, it adds it to Every row. The only thing I can think of is that my WHERE condition is wrong, but I can't seem to figure it out. Maybe it just needs fresh eyes, or maybe I'm heading in Completely the wrong direction.
Also, any specific instructions on security, a better way to do it, etc. would be very helpful. I'm learning PHP on the fly and could use a helping hand. :)
<?php
$query = "SELECT * FROM client_information";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$which_ad = $row['ID'];?>
<b>Name:</b> <? echo $row['billing_name']; ?> <br>
<b>Job Type:</b> <? echo $row['job_type']; ?> <br>
<b>Size:</b> <? echo $row['size']; ?> <br>
<b>Text:</b> <? echo $row['text']; ?> <br>
<b>Notes:</b> <? echo $notes; ?> <br>
<br><br>
<form action="small_update.php" method="POST">
<strong>Email Message:</strong><br>
<textarea rows="8" cols="60" name="email_message"></textarea>
<input type="submit" name="submit" value="Submit"></form>
<?
$email_message = htmlspecialchars ("{$_POST['email_message']}", ENT_QUOTES);
if (mysql_errno() != 0) {
die(mysql_error());
}
mysql_query(
"UPDATE client_information
SET email_message='$email_message'
WHERE ID='$which_ad'"
);
if (mysql_errno() != 0) {
die(mysql_error());
}
}
?>
You don't specify the id in your form:
<form action="small_update.php" method="POST">
<strong>Email Message:</strong><br>
<textarea rows="8" cols="60" name="email_message"></textarea>
<input type="hidden" name="id" value="<?php echo $which_ad; ?>">
<input type="submit" name="submit" value="Submit">
</form>
you need to also make sure you know what id was submitted:
"UPDATE client_information
SET email_message='$email_message'
WHERE ID='$_POST['id']'"
Of course, you're wide open to attacks like this as everyone else is saying. You need to look into mysqli or pdo to sanitize your input...
Ans also upon inspection you're evaluating your post data in the loop. Don't do that. Just do your evaluation before everything else is processed on the page...
<?php
if($_POST)
{
//run processing here
}
// do your fetch code here and display the forms...

Categories