i need some help here.
I need to create this grid that allows the user to select a winner, and that winner will be stored into a table in my database.
Im geeting all done, until the point were i need to me this Radio Button working, it inserts an empty row.
This my form...
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" .$row['id']."</td>";
echo "<td>" . "<input type='radio' name='premio' value='".$row['id']."'>" ."</td>";
echo "<td >" .$row['nombre']. "</td>";
echo "<td name='correo'>" . $row['correo'] . "</td>";
echo "<td name='telefono'>" . $row['telefono'] . "</td>";
echo "<td>" . $row['ar1'] . "</td>";
echo "<td>" . $row['ar2'] . "</td>";
echo "<td>" . $row['br1'] . "</td>";
echo "<td>" . $row['br2'] . "</td>";
echo "<td>" . $row['cr1'] . "</td>";
echo "<td>" . $row['dr1'] . "</td>";
echo "<td>" . $row['dr2'] . "</td>";
echo "<td>" . $row['dr3'] . "</td>";
echo "<td>" . $row['er1'] . "</td>";
echo "<td>" . $row['er2'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "<input type='submit' value='enviar'/>";
mysqli_close($con);
?>
</form>
</div>
And the link if you want to check it out.
http://www.gifted.cl/clientes/servier/registro_evento_1.php
php
<?php
$con=mysqli_connect("localhost","user","pass","db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO ganadoresevento (nombre,correo,telefono)
VALUES
('$_POST[nombre]','$_POST[correo]','$_POST[telefono]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "Respuestas registradas";
mysqli_close($con);
?>
Thanks in advance
echo "<td>" . "<input type='radio' name='premio' value='".$row['id']."'>" ."</td>";
echo "<td>" . "<input type='radio' name='nombre' value='".$row['nombre']."'>" ."</td>";
echo "<td>" . "<input type='radio' name='correo' value='".$row['correo']."'>" ."</td>";
echo "<td>" . "<input type='radio' name='telefono' value='".$row['telefono']."'>" ."</td>";
Don't really know what inputs you want to use but this uses radio button. You could use <input type="text"> or <input type="hidden">
Have a look at prepared statement also http://www.php.net/manual/en/mysqli.prepare.php
Related
I seem to be having an issue with my HTML formatting. I am calling the database to display all information within the table on a webpage. I have got the information coming down into the table. Unfortunately the table is adding an additional row to the bottom of the table making 4 rows when in fact there is only 3 rows displaying in the database.
Any idea as to why?
<?php
//connect to the server
$link = mysql_connect('*****', '*****', '****');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('******');
$query = mysql_query("SELECT * FROM tablename");
echo "<table border='1'>
<tr>
<td>Date</td>
<td>Region</td>
<td>Cameraman</td>
<td>Livestream?</td>
<td>Event Title</td>
<td>Lecturer</td>
<td>Time</td>
<td>Speaker</td>
<td>ICE Contact</td>
<td>Venue Address</td>
<td>Venue Contact</td>
<td>Additional Comments</td>
<td>On App?</td>
</tr>";
WHILE($rows = mysql_fetch_array($query)):
echo "<tr>";
echo "<td>" . $rows['date'] . "</td>";
echo "<td>" . $rows['region'] . "</td>";
echo "<td>" . $rows['cameraman'] . "</td>";
echo "<td>" . $rows['livestream'] . "</td>";
echo "<td>" . $rows['eventitle'] . "</td>";
echo "<td>" . $rows['lecturer'] . "</td>";
echo "<td>" . $rows['time'] . "</td>";
echo "<td>" . $rows['speaker'] . "</td>";
echo "<td>" . $rows['icecontact'] . "</td>";
echo "<td>" . $rows['venueaddress'] . "</td>";
echo "<td>" . $rows['venuecontact'] . "</td>";
echo "<td>" . $rows['additioncomments'] . "</td>";
echo "<td>" . $rows['onapp'] . "</td>";
echo "</tr>";
endwhile;
echo "</table>";
?>
I have added the link to the page below.
http://cpdonline.tv/spreadsheet/spreadsheet.php
Update your while loop with the following:
WHILE($rows = mysql_fetch_array($query)):
$rows = array_filter($rows);
if (!empty($rows)) {
echo "<tr>";
echo "<td>" . $rows['date'] . "</td>";
echo "<td>" . $rows['region'] . "</td>";
echo "<td>" . $rows['cameraman'] . "</td>";
echo "<td>" . $rows['livestream'] . "</td>";
echo "<td>" . $rows['eventitle'] . "</td>";
echo "<td>" . $rows['lecturer'] . "</td>";
echo "<td>" . $rows['time'] . "</td>";
echo "<td>" . $rows['speaker'] . "</td>";
echo "<td>" . $rows['icecontact'] . "</td>";
echo "<td>" . $rows['venueaddress'] . "</td>";
echo "<td>" . $rows['venuecontact'] . "</td>";
echo "<td>" . $rows['additioncomments'] . "</td>";
echo "<td>" . $rows['onapp'] . "</td>";
echo "</tr>";
}
endwhile;
echo "</table>";
array_filter() function's default behavior will remove all values from array which are equal to null, 0, '' or false.
It seems that you have empty data in your database, as many lines are empty
Check your database for empty records or run the query
DELETE FROM table_name WHERE column='';
I have a MySQL table that is displayed in the webpage and every row has a submit button. When the submit button for a particular row is clicked, the first element i.e. row id should be retrieved. Should I use arrays to store the data or can I obtain the row id from the table I've echoed on screen. Here's the code:
echo "<table>";
while($row = mysqli_fetch_row($result))
{
echo "<tr>";
echo "<td align='center' name='bus_id'>" . $row[0] . "</td>";
echo "<td>" . $row[1] . "</td>";
echo "<td>" . $row[2] . "</td>";
echo "<td>" . $row[3] . "</td>";
echo "<td>" . $row[4] . "</td>";
echo "<td>" . $row[5] . "</td>";
echo "<td>" . $row[6] . "</td>";
echo "<td>" . $row[7] . "</td>";
echo "<td>" . $row[7] . "</td>";
echo "<td><form name='f1' method='post' action='schedule_list.php'>
<input type='submit' name='book' value='Book'></form></td>";
echo "</tr>";
}
echo "</table";
Use a a button with type submit to submit the ID as follows (and still display a meaningful button title
echo "<td><form name='f1' method='post' action='schedule_list.php'>
<button type='submit' name='book' value='".$row[0]."'>Book</button></form></td>";
I'm trying to update specific rows in a table that is built from a form. I am using checkboxes but the whole table is being updated instead of the specific row that I am filling in. I tried ISSET to see if I could get it to only update when the boxes were ticked, but that didn't work. Here's what I have (obviously not the whole code, but I can provide more as needed):
<?php
include 'connect.php';
// All possible parameters
$params = array(
'Student',
'homeroom',
'teacher'
);
$wheres = array();
foreach ($params as $param) {
// Is the param set?
// If so, let's add it to our list of WHERE clauses
if (isset($_GET[$param]) && !empty($_GET[$param])) {
$wheres[] = sprintf("%s LIKE '%%%s%%'", $param, mysqli_real_escape_string($connect, $_GET[$param]));
}
}
if ($db_found) {
// Now let's make the SQL.
$SQL = 'SELECT * FROM studentlist';
// We only want to add the WHERE clause if we had some parameters passed
if (count($wheres) > 0) {
$SQL .= ' WHERE ' . implode(' OR ', $wheres);
}
$result = mysqli_query($connect, $SQL);
while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) {
echo "<tr>";
echo "<td>" . $row['Student'] . "</td>";
echo "<td>" . $row['homeroom'] . "</td>";
echo "<td>" . "<input type='checkbox' name='responsibility' value='E'>" . "</td>";
echo "<td>" . "<input type='checkbox' name='responsibility' value='G'>" . "</td>";
echo "<td>" . "<input type='checkbox' name='responsibility' value='S'>" . "</td>";
echo "<td>" . "<input type='checkbox' name='responsibility' value='N'>" . "</td>";
echo "<td>" . "" . "</td>";
echo "<td>" . "<input type='checkbox' name='organization' value='E'>" . "</td>";
echo "<td>" . "<input type='checkbox' name='organization' value='G'>" . "</td>";
echo "<td>" . "<input type='checkbox' name='organization' value='S'>" . "</td>";
echo "<td>" . "<input type='checkbox' name='organization' value='N'>" . "</td>";
echo "<td>" . "" . "</td>";
echo "<td>" . "<input type='checkbox' name='independentwork' value='E'>" . "</td>";
echo "<td>" . "<input type='checkbox' name='independentwork' value='G'>" . "</td>";
echo "<td>" . "<input type='checkbox' name='independentwork' value='S'>" . "</td>";
echo "<td>" . "<input type='checkbox' name='independentwork' value='N'>" . "</td>";
echo "<td>" . "" . "</td>";
echo "<td>" . "<input type='checkbox' name='collaboration' value='E'>" . "</td>";
echo "<td>" . "<input type='checkbox' name='collaboration' value='G'>" . "</td>";
echo "<td>" . "<input type='checkbox' name='collaboration' value='S'>" . "</td>";
echo "<td>" . "<input type='checkbox' name='collaboration' value='N'>" . "</td>";
echo "<td>" . "" . "</td>";
echo "<td>" . "<input type='checkbox' name='initiative' value='E'>" . "</td>";
echo "<td>" . "<input type='checkbox' name='initiative' value='G'>" . "</td>";
echo "<td>" . "<input type='checkbox' name='initiative' value='S'>" . "</td>";
echo "<td>" . "<input type='checkbox' name='initiative' value='N'>" . "</td>";
echo "<td>" . "" . "</td>";
echo "<td>" . "<input type='checkbox' name='selfregulation' value='E'>" . "</td>";
echo "<td>" . "<input type='checkbox' name='selfregulation' value='G'>" . "</td>";
echo "<td>" . "<input type='checkbox' name='selfregulation' value='S'>" . "</td>";
echo "<td>" . "<input type='checkbox' name='selfregulation' value='N'>" . "</td>";
echo "</tr>";
And then I have this as my update:
<?php
include 'connect.php';
// Get values from form
$resp=$_POST['responsibility'];
$org=$_POST['organization'];
$ind=$_POST['independentwork'];
$coll=$_POST['collaboration'];
$init=$_POST['initiative'];
$self=$_POST['selfregulation'];
// Insert data into mysqli
$sql = "UPDATE studentlist
SET responsibility='$resp', organization='$org', independentwork='$ind',
collaboration='$coll', initiative='$init', selfregulation='$self'";
$result=mysqli_query($connect, $sql);
// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
}
else {
echo "ERROR";
}
?>
<?php
// close connection
mysqli_close($connect);
?>
I understand that I have all the checkboxes named the same thing, but I'm not sure how to move away from that. And yes, I am new to this!!!!!
You have to add to the end of your querya WHERE condition to upsate just the row you want, if you use for example StudentId as the identifier of your Student then you write :
$sql = "UPDATE studentlistSET responsibility='$resp', organization='$org', independentwork='$ind',collaboration='$coll', initiative='$init', selfregulation='$self' WHERE StudentId='$studentId' ;
I'm trying to pass some variables from a link but for some reason it only reads one of them. Below is the link and the code. roundNo shows up fine.
archery/viewround.php?gameNo=1&roundNo=7
echo $_GET['gameNo'];
echo $_GET['roundNo'];
echo "<table border><tr><th>Round ID</th><th>Match Name</th><th>Player Name</th><th>Shot 1</th><th>Shot 2</th><th>Shot 3</th><th>Shot 4</th><th>Shot 5</th><th>Shot 6</th><th>Shot 7</th><th>Shot 8</th><th>Shot 9</th><th>Shot 10</th></tr>";
while ($row = mysql_fetch_array($sql)) {
$tempPlayerName = getnamewithid($row["player_id"]);
$tempMatchName = getmatchnamewithid($_GET['gameNo']);
echo "<tr>";
echo '<td>' . $row["round_id"].'</td>';
echo "<td>" . $tempMatchName. "</td>";
echo "<td>" . $tempPlayerName. "</td>";
echo "<td>" . $row["shot_1"] . "</td>";
echo "<td>" . $row["shot_2"] . "</td>";
echo "<td>" . $row["shot_3"] . "</td>";
echo "<td>" . $row["shot_4"] . "</td>";
echo "<td>" . $row["shot_5"] . "</td>";
echo "<td>" . $row["shot_6"] . "</td>";
echo "<td>" . $row["shot_7"] . "</td>";
echo "<td>" . $row["shot_8"] . "</td>";
echo "<td>" . $row["shot_9"] . "</td>";
echo "<td>" . $row["shot_10"] . "</td>";
echo "</tr>";
$i++;
}
echo "</table>";
$sql = ""
$result = mysql_query($sql);
while ($row = #mysql_fetch_row($result)) {
echo "<tr bgcolor='#dcdcdc'>" ;
echo "<td>$row[0] </td>";
echo "<td>" . $row[1] . "</td>";
echo "<td>" . $row[2] . "</td>";
echo "<td>" . $row[3] . "</td>";
echo "<td>" . $row[4] . "</td>";
echo "<td>" . $row[5] . "</td>";
echo "<td>" . $row[6] . "</td>";
}
when I run the above php code on my local mysql server , my last row value is correctly displayed.
But when the same is deployed on the server and access from client PC , last cell value is empty.
Please help me how to sort this issue out.
I think you're missing the trailing </tr> from your code, that could potentially do it:
while ($row = #mysql_fetch_row($result)) {
echo "<tr bgcolor='#dcdcdc'>" ;
echo "<td>$row[0] </td>";
echo "<td>" . $row[1] . "</td>";
echo "<td>" . $row[2] . "</td>";
echo "<td>" . $row[3] . "</td>";
echo "<td>" . $row[4] . "</td>";
echo "<td>" . $row[5] . "</td>";
echo "<td>" . $row[6] . "</td>";
echo "</tr>";
}
I'd check the database to make sure you have all of the rows in your table. That would pretty much have to be the problem.
Make sure you have at least 7 columns in there.