How do I display results from a database into an input area? - php

<?php
$i = 0;
while ($row = $res->fetchRow()) {
$i++;
echo '<tr>';
echo '<td><input value='{$row['moduleCode']}' /> </td>';
echo '</tr>';
}
?>
As you can see in my php code i have tried echoing the results from the database into the input area via the 'value', however this keeps coming up with a syntax error? can someone show me where I have gone wrong?

Use double-quoted strings if you want variables to be parsed within :
echo "<td><input value='{$row['moduleCode']}' /> </td>";
or extract your variables :
echo '<td><input value="'.$row['moduleCode'].'" /> </td>';

Related

How to give the checkbox a value for each data that called from database and how to post it

I don't know if this can be done in php. I have an equipment list that is call from database. User can select any equipment that they want to borrow by selecting the checkbox. I have checkbox named 'list'. The question is how to put checkbox value and post the checkbox value?? because I want to save the selected item(checked) into database.
<?php
//include("connect.php");
$sql = mysql_query("SELECT * FROM equipment WHERE equip_name_desc='$search'");
$row = mysql_num_rows($sql);
if ($row >= 1){
echo '<table border="1"><tr>';
echo '<td align="center" width="40">NO</td>';
echo '<td width="200">DESCRIPTION</td>';
echo '<td width="120">SERIAL NUM.</td>';
echo '<td width="120">REF. NUM.</td>';
echo '<td width="120">PRICE</td>';
echo '<td width="120">STATUS</td>';
echo '<td width="80">BORROW</td>';
echo '</tr>';
$index=1;
while($a = mysql_fetch_array($sql))
{
echo '<tr>';
echo '<td align="center">'.$index.'</td>';
echo '<td>'.$a['equip_desc'].'</td>';
echo '<td>'.$a['equip_sn'].'</td>';
echo '<td>'.$a['equip_ref'].'</td>';
echo '<td>'.$a['equip_unit_price'].'</td>';
echo '<td>'.$a['equip_status'].'</td>';
echo '<td>'?><input type="checkbox" name="list" /><?php
echo '</tr>';
$index++;
}
echo '</table>';
}
?>
Well, pretty weird thing, but ill try to understand. I suppose the serialnumber is the unique primary. So you could use that as a reference for all the items in the array:
echo '<td><input type="checkbox" name="list" value="$a[\'equip_sn\']></td>';
this is how you put the checkbox value
echo '<td>'?><input type="checkbox" name="list" value="'.$a['your field'].'" />
to post the check box value
assign it to a variable
$variablename=$_POST['list']=="$a[0]" //take the values stored in the array

Using Checkbox in php while inserting multiple values

I have tried producing a form with the loop, when I process the input the checkbox values are not retrieving, it says invalid index.
Form:
for($i=0;$i<$count;i++){
echo '<td>';
echo '<input name="att[$i]" type="checkbox" id="att'.$i.'" value="1"/>';
echo '</td>';
echo '<td>';
echo '<input name="rno[$i]" type="text" id="rno'.$i.'"/>';
echo '</td>';
}
Retrieveing:
for($j=0;$j<$count;j++){
echo $rno=$_POST['rno'][$j];
echo $rno=$_POST['att'][$j];
}
I don't know where the problem is...
The error message invalid index "att". What is the problem?
It should be 'att' (string) rather than att:
for($j=0;$j<$count;j++){
echo $rno=$_POST['rno'][$j];
echo $rno=$_POST['att'][$j];
}

Can not get PHP to post info to database

I've written a script to post information into my database for I can't get it to post and need help pointing out what I'm missing.
my php post for looks like this:
$query = "SELECT * from departments";
$res = mysql_query($query);
echo '<div id="department" >';
echo '<form action="depedit.php" method="post">';
echo '<input type="text" placeholder="Search departments">';
echo '<br>';
echo '</form>';
echo '</div>';
echo '<div id="depadd">';
echo '<form>';
echo '<table width="0" border="0">';
echo '<tr>';
echo '<td>Name:</td>';
echo '</tr>';
echo '<tr>';
echo '<td>';
echo "<select name='depid'>";
while ($row = mysql_fetch_array($res)) {
echo "<option value='".$row['id']."'>".$row['depname']."</option>";
}
echo "</select>";
echo '</td>';
echo ' </tr>';
echo '<tr>';
echo '<td> </td>';
echo '</tr>';
echo ' <tr>';
echo '<td><label class="limit">Select Limit for active courses in Learning Locker:</label></td>';
echo ' </tr>';
echo ' <tr>';
echo '<td>';
echo "<select name='courselimit'>";
echo "<option value='1'>1</option>";
echo "<option value='2'>2</option>";
echo "<option value='3'>3</option>";
echo "<option value='4'>4</option>";
echo "<option value='5'>5</option>";
echo "<option value='0'>Unlimited</option>";
echo "</select>";
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<td> </td>';
echo '</tr>';
echo '<tr>';
echo '<td><input type="radio" id="1" name="senabled" value="1"/><label for="c1" class="required">Required<br>(Study Shredder Feature Enabled)</br></label></td>';
echo '<td><input type="radio" id="0" name="senabled" value="0"/><label for="c1" class="optional">Optional</label></td>';
echo '</tr>';
echo '<tr>';
echo '<td><input type="hidden" name="orgid" value="'.$adminorgid.'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td><input type="hidden" name="createdby" value="'.$userid.'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td><input type="hidden" name="timecreated" value="'.time(now).'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td> </td>';
echo '</tr>';
echo ' <tr>';
echo ' <td><button type="submit" class="btn">Submit</button></td>';
echo ' </tr>';
echo '</table>';
echo "</form>";
echo '</div>';
depedit.php looks like this:
$adddep = "INSERT INTO organization_dep (orgid,depid,courselimit,senabled,createdby,timecreated) VALUES ('".$_POST["orgid"].",".$_POST["depid"].",".$_POST["courselimit"].",".$_POST["senabled"].",".$_POST["createdby"].",".$_POST["timecreated"]."')";
$res = mysql_query($adddep);
if ($res === TRUE) {
echo "Department added successfully";
} else{
printf("Could not create department");
}
the correct information seems like its getting passed as the url displays the following information in it:
/index.php?depid=6&courselimit=3&senabled=1&orgid=9&createdby=1129&timecreated=1364005206
any help with this would be greatly appreciated as I'm sure its something simple that I'm just over looking.
You want to get the values from the $_POST
('".$_POST["orgid"].",".$_POST["depid"].",".$_POST["courselimit"].",".$_POST["senabled"].",".$_POST["createdby"].",".$_POST["timecreated"]."')";
but your saying it is passed through the URL, this looks like you need to use $_GET to get the values
--- and one small tip you can use echo to print multiple lines it works. as long as you close it, in the end
e.g
echo " <html>
<body>
</body>
</html>
";
I think you have a bunch of things to get right before this will be working:
Only the first form, which seems to be for searching departments, have method and action attributes
Since the second form does not have the action attribute, it is not submitting to depedit.php but to the same page that has the form.
Since the second form does not have the method attribute, it defaults to GET, and you are trying to read out POST variables in your PHP. If it was not using GET, you would not see those params in the resulting URL.
In your SQL insert statement, you must have single quotes around every single text value but not around int values. Now you have one single quote before the first value and one before the last which makes no sense.
First: this portion will not be POSTed "depedit.php" because there is no submit
echo '<form action="depedit.php" method="post">';
echo '<input type="text" placeholder="Search departments">';
echo '<br>';
echo '</form>';
Second: this will never be POSTed to depedit.php, since your <form> does not have an action specifying "depedit.php"
echo '<form>';
echo '<table width="0" border="0">';
echo '<tr>';
//other codes
echo "</form>";
maybe you mean to remove the first echo '</form>'(line 7) and the second <form>(line 10)

Getting two values from a mysql table using a checkbox

So I have a mysql table for the charges of a hospital. My program currently only gets the price of the checked procedure. But now, I also want to get the procedure name when it is checked.
while($row = mysql_fetch_array($result)){
echo '
<tr>
<td>'.$row[0].'</td>
<td>'.$row[1].'</td>
<td>'.$row[2].'</td>';
$price=$row['price'];
echo '<td><input type="checkbox" name="er[]" value="'.$price.'"></td>';
echo "</tr>";
}
echo '</table>';
So basically I need to edit this part of the code:
$price=$row['price'];
echo '<td><input type="checkbox" name="er[]" value="'.$price.'">
It only gets the row for price. Can the value attribute have two values? I can't just make another checkbox cause that would be inappropriate.
change the code like this
<?
while($row = mysql_fetch_array($result)){
echo '
<tr>
<td>'.$row[0].'</td>
<td>'.$row[1].'</td>
<td>'.$row[2].'</td>';
$price=$row['price'];
$procedure_name=$row['$procedure_name'];
echo '<td><input type="checkbox" name="er" value="'.$price.','.$procedure_name.'"></td>';
echo "</tr>";
}
echo '</table>';
?>
Then use explode()
You could simply put the name in another attribute (like title) and whenever you need to get the name you can use some javascript to retreive it.
$price=$row['price'];
echo '<td><input type="checkbox" name="er[]" value="'.$price.'" title="'.$name.'">

a array of php with multiple textboxes

basically i have a database, which contains a random amount of questions
i have then printed this out as a php table using a query. there is as many textboxes to input marks as there are questions in the database.
I want to create an array of my inputted data and then update the database with the marks in.
Here is my code
echo "<table border = '0' cellpadding ='10px'>";
echo "<tr>
<td> Question <td>Mark</td><td>Criteria</td>
<td>Feedback</td>
</tr>";
while ($row = mysql_fetch_array($result))
{
$question[]=$rows['question'];
echo "<tr>";
echo "<td>". $row['question']. "</td>";
echo "<td>" ."<input type = 'text' name = 'mark[]' size = '1' value = '0' id = 'mark'/>/". $row['maxMark'] . "</td>";
$maxMark[] = $row['maxMark'];
echo "<td>".$row['criteria']."</td>";
echo "<td>" . "<textarea name = 'feedback[]' id= 'feedback'>Enter Feedback here</textarea>". "</td>";
echo "</tr>";
}
echo "</table>";
echo "</tr>\n";
echo "</table>";
I am not sure how to create the array, with the marks you input. please help.
In short i want to populate an array with the marks i input
This is rather easy to be done. All you have to do is setting the attribute name of your input controls and add an index.
Example:
<input type="text" name="name[0]" /><input type="text" name="mark[0]" />
<input type="text" name="name[1]" /><input type="text" name="mark[1]" />
The post (or get) data your script receives will then contain an array instead of a single variable.

Categories