i am trying to add my form values in to my data base but i am getting error like
Undefined index: submit in line no.120
some of code is,
<?php
echo "</tr></table></form>";
$conn = mysql_connect('localhost','root','');
mysql_select_db('itcompanylist',$conn);
$result = mysql_query("SELECT state_name FROM `states` WHERE c_id =1");
$i = 0;
echo "<form method='post' action=''><table border='1' ><tr>";
while ($row = mysql_fetch_row($result)){
// echo "<td><a href='#' onclick='someFunction()'>" .$row['0']. "</a> </td>";
echo '<td><input type="submit" name="submit" value="'.$row['0'].'"></td>';
if ($i++ == 2)
{
echo "</tr><tr>";
$i=0;
}
}
echo "</tr></table></form>";
?>
action is fire on same page an page is below,when action fire into page i am gatting error :Undefined index: submit in line no.120
<?php
mysql_connect("localhost","root","");//database connection
mysql_select_db("itcompanylist");
$query = "SELECT s_id FROM states WHERE `state_name` = '".$_POST['submit']."'";
$result1 = mysql_query($query);
$row = mysql_fetch_array($result1);
$result2 = mysql_query("SELECT city_name FROM `city` WHERE s_id ='".$row['s_id']."'");
$i = 0;
echo "<form method='post' action='demo2.php'><table border='1' ><tr>";
while ($row = mysql_fetch_row($result2)){
echo '<td><input type="submit" name="ok" value="'.$row['0'].'"></td>';
}
echo "</tr></table></form>";
?>
please replace the code
echo '<td><input type="submit" name="submit" value="'.$row['0'].'"></td>';
instead of
echo '<td><input type="submit" name="ok" value="'.$row['0'].'"></td>';
beacuse you called $_post['submit']..
You are trying to use a variable that doesn't exist $_POST['submit']. use:
$query = "SELECT s_id FROM states WHEREstate_name= '".$_POST['ok']."'";
Be sure to migrate to mysqli_* see Hank's comment to your question.
Related
This is the code that I've got to search and it just appears next to each other. Can I put html within php and how would this be done? Or should I make a table below the form?
<?php
include ('database_conn.php');
$output = '';
if(isset($_POST['search'])) {
$search = $_POST['search'];
$search = preg_replace("#[^0-9a-z]i#","", $search);
$query = mysqli_query($conn, "SELECT * FROM attendance WHERE stud_id LIKE '%$search%'") or die ("Could not search");
$count = mysqli_num_rows($query);
if($count == 0){
$output = "There was no search results!";
}else{
while ($row = mysqli_fetch_array($query)) {
$stud_id = $row ['stud_id'];
$module = $row ['module'];
$attendance_status = $row ['attendance_status'];
$output .='<div> '.$stud_id.''.$module.''.$attendance_status.'</div>';
}
}
}
?>
This is the form that I've got in the HTML to search
<form action ="CM0671_attendance.php" method = "post">
<input name="search" type="text" size="30" placeholder="Student ID"/>
<input class="btn btn-primary" type="submit" value="Search"/>
</form>
Replace your while with this:
echo "<table>";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>Module</th>";
echo "<th>Status</th>";
echo "</tr>";
while ($row = mysqli_fetch_array($query)) {
$stud_id = $row ['stud_id'];
$module = $row ['module'];
$attendance_status = $row ['attendance_status'];
echo "<tr>";
echo "<td>{$stud_id}</td>";
echo "<td>{$module}</td>";
echo "<td>{$attendance_status}</td>";
echo "</tr>";
}
echo "</table><br>";
I am using the following code to display certain rows from my database table:
<?php
$searchtype=$_POST['searchtype'];
$searchterm=$_POST['searchterm'];
$searchterm= trim($searchterm);
if (!$searchtype || !$searchterm)
{
echo 'Error';
exit;
}
if (!get_magic_quotes_gpc())
{
$searchtype = addslashes($searchtype);
$searchterm = addslashes($searchterm);
}
$db = include "connect2db.php";
$query = "select * from notes where ".$searchtype." like '%".$searchterm."%'";
$result = $db->query($query);
$num_results = $result->num_rows;
echo '<p>Number of rows found: '.$num_results.'</p>';
for ($i=0; $i <$num_results; $i++)
{
$row = $result->fetch_assoc();
echo '<i>';
echo stripslashes($row['date']);
echo '</i><br /> ';
echo '<b>';
echo stripslashes($row['notetitle']);
echo '</b><br /> ';
echo stripslashes($row['note']);
echo '<br /><br /> ';
echo '</p>';
}
$result->free();
$db->close();
?>
Now I would like to display an edit-link for each row displayed, that can open a new page in which it is possible to edit a specific row. I already have the code that lets you edit the row:
<?php
if ($_REQUEST['save']=="Save") { // is data submitted?
// create variables
$noteid = $_REQUEST['noteid'];
$coursename = $_REQUEST['coursename'];
$notetitle = $_REQUEST['notetitle'];
$note = $_REQUEST['note'];
$query = "UPDATE notes SET ";
$query .= "coursename='$coursename', ";
$query .= "notetitle='$notetitle', ";
$query .= "note='$note' ";
$query .= "WHERE noteid='$noteid'";
$result = $db->query($query);
} elseif ($_REQUEST['delete']=="Delete") { // is data to be removed?
$noteid = $_REQUEST['noteid'];
$query="DELETE FROM notes WHERE noteid='$noteid'";
$result = $db->query($query);
}
?>
<div class="formular">
<div class="row1">
<p>Id</p>
<p>Notetitle</p>
<p>Note</p>
</div>
<?php
$query = "SELECT * FROM notes ORDER BY noteid DESC";
$result = $db->query($query);
while ($row = mysqli_fetch_array($result)) {
echo "<form ".$_SERVER['PHP_SELF']." name='edit-form' method='post' class='row1'>\n";
echo "<p class='align_top padding_top'>".$row['noteid']."<input type='hidden' name='noteid' value='".$row['noteid']."' /></p>\n";
echo "<p class='align_top'><input type='text' name='notetitle' value='".$row['notetitle']."' /></p>\n";
echo "<p><textarea name='note' rows='10' cols='50'>".$row['note']."</textarea></p>\n";
echo "<p><input type='submit' name='save' value='Save' /></p>";
echo "<p><input type='submit' name='delete' value='Delete' /></p>";
echo "</form>\n";
}
echo '</div>';
$result->free();
$db->close();
?>
What I am struggling with is how to display an edit-link for each row that lets you open a page where you can edit/delete the content of only that row.
I hope someone can help, I am very new at this.
Thank you!
Add a button next to each row that opens an edit page (or modal) with the id inside, example: <button onclick="edit('randomId')">Edit RandomId </button>
You could implement something different that accepts the unique id of that specific row and open a new page or modal with it.
I have a question regarding my project.
I need to make a database update section which pulls down from the server and showing all the values then update it to the database.
My approach is, getting the value and then display it as a check box. When user select one of the checkboxes, it will change the character from N to Y and then disable it button so people cannot make further update. I can successfully implemented the pulldown and getting the info from the server. but i dont know how to implement the checkbox process, update it to the server and set disable on it.
Please help me
This is the JS section on update_fab.php
<script>
// ===================================== //
// ===================================== //
$(function(){
// SHOW RECORD
$('#show').click(function () {
$.post('data2.php', {
action: "show",
"hm": $('#headmark').val()
}, function(res) {
$('#result').html(res);
});
});
});
</script>
<?php
$result = oci_parse($conn, 'SELECT HEAD_MARK FROM FABRICATION');
oci_execute($result);
echo '<div class = "wrapper">';
echo '<div id = "contact_form">';
echo '<span>Head mark</span><label><SELECT name="headmark" id="headmark">'.'<br>';
echo '<OPTION VALUE=" ">'."".'</OPTION>';
while ($row = oci_fetch_array($result, OCI_ASSOC)) {
$HM = $row ['HEAD_MARK'];
echo "<OPTION VALUE='$HM'>$HM</OPTION>";
}
echo '</SELECT></label><br />';
?>
<input class ="showButton" type="button" value="show" name="SHOW RECORD" id="show"/>
and on the data2.php
// IF SHOW KEY HAS BEEN PRESSED
if ($_POST['action'] == 'show') {
$sql = "SELECT * FROM SUB_MASTER_DRAWING
WHERE SUB_MASTER_DRAWING.HEAD_MARK = '{$_POST["hm"]}'";
$query = oci_parse($conn, $sql);
$query_exec = oci_execute($query);
echo "<table border='1'>";
while ($row = oci_fetch_assoc($query)) {
echo '<div id="content">';
echo '<table cellspacing = "0"';
echo '<tr><th>Head Mark</th>
<th>Cutting</th>
<th>Assembly</th>
<th>Welding</th>
<th>Drilling</th>
<th>Finishing</th>
<th>Update</tr>';
echo "<tr><td><b>$row[HEAD_MARK]/$row[ID]</b></td>
<td><input type='checkbox' name='check'/></td>
<td><input type='checkbox' name='check'/></td>
<td><input type='checkbox' name='check'/></td>
<td><input type='checkbox' name='check'/></td>
<td><input type='checkbox' name='check'/></td>
<td><input type='button' value='UPDATE' name='updatefab'/></td>
</tr>";
if (isset($_POST['updatefab'])) {
echo '<script type = "text/javascript">submit pressed !</script>';
}
echo '<table cellspacing = "0"';
echo '</div>';
}
echo "</table>";
}
Good evening, I'm trying to make a single php page wich can edit/delete multiple rows in mysql:
<html>
<head>
<title>Update/Delete Test Page</title>
</head>
<body>
<?
include 'connect.php';
if(isset($_POST['edit'])) // from button name="delete"
{
$title = $_POST['title'];
$description = $_POST['description'];
if(!empty($_POST['checkbox'])) {
foreach($_POST['checkbox'] as $check) {
$ed = $check;
$sql = "UPDATE events SET title = '$title', description ='$description' WHERE id = $ed";
}
}
if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con);
}
?>
<?php
include ("connect.php");
if(isset($_POST['delete'])) // from button name="delete"
{
if(!empty($_POST['checkbox'])) {
foreach($_POST['checkbox'] as $check) {
$del = $check;
$sql = "DELETE from events where id = $del";
$result = $mysqli->query($sql) or die(mysqli_error($mysqli));
}
}
?>
<?php
include 'connect.php';
$query = 'SELECT id, title, description FROM events WHERE evdate = "1/9/2013" order by title asc';
$result2 = $mysqli->query($query) or die(mysqli_error($mysqli));
echo '<br><br><br>';
echo '<b><div align="center"> "1/9/2013"</div></b>';
if ($result2) {
// create a new form and then put the results
// into a table.
echo "<form method='post' action=".$_SERVER['PHP_SELF'].">";
echo "<table cellspacing='0' cellpadding='3'>
<th align='left'>Interval orar</th>
<th align='left'>Eveniment</th>
<th align='left'></th>
";
while ($row = $result2->fetch_object()) {
$title = $row->title;
$description = $row->description;
$id = $row->id;
//put each record into a new table row with a checkbox
echo "
<tr>
<td align='left'><input type='text' name='title' size='20' value='$title'></td>
<td align='left'><input type='text' name='description' size='50' value='$description'></td>
<td align='left'><input type='checkbox' name='checkbox[]' id='checkbox[]' value=$id />
</tr>
";
}
// when the loop is complete, close off the list.
echo "</table>
<p>
<input id='edit' type='submit' class='button' name='edit' value='Edit'/>
<input id='delete' type='submit' class='button' name='delete' value='Delete'/>
</p>
</form>";
}
?>
</body>
</html>
Connect.php looks like this:
<?php
$db_host = "localhost";
$db_username = "calendar";
$db_pass = "calendar";
$db_name = "ecalendar";
$con = mysql_connect ("$db_host", "$db_username", "$db_pass") or die ("could not connect to mysql database");
mysql_select_db("$db_name") or die ("no database");
$mysqli = new MySQLi($db_host, $db_username, $db_pass, $db_name) or die(mysqli_error());
?>
The delete works fine, but the edit doesn't do anything...
Table looks like this:
Start/End Time Event (Checkbox is here)
08:00-10:00 test1 X
10:00-12:00 test2 X
When I try to edit test2 to test2xx I get this in Firebug POST :
title=08%3A00-10%3A00&description=test1&title=10%3A00-12%3A00&description=test2xx&checkbox%5B%5D=53&edit=Edit
If I delete I get this (and it works)
title=08%3A00-10%3A00&description=test1&title=10%3A00-12%3A00&description=test2&checkbox%5B%5D=53&delete=Delete
Edit works but only for the last row (test2), if I try to edit the row above (test1), it insted updates it with the values of the last row (test2)
Guess you need del_id not id
$sql = "UPDATE events SET title = '$title', description ='$description' WHERE id = $del_id ";
1-look your value value=$id in your chekbob
change it to value= '$id'
2-and also , the delete query is with mysqli
but the update query is with mysql ??
3- u dont have to include connect.php 3 times
4- also what previous answer about the variable $del_id.
5- u are missing closing tag td
6- u are missing closing }
7- try this
<html>
<head>
<title>Update/Delete Test Page</title>
</head>
<body>
<?php
include 'connect.php';
if(isset($_POST['edit'])) // from button name="edit"
{
$checkbox = $_POST['checkbox']; //from name="checkbox[]"
$countCheck = count($_POST['checkbox']);
for($i=0;$i<$countCheck;$i++)
{
$del_id = $checkbox[$i];
$sql3 = "UPDATE events SET title = '$title', description ='$description' WHERE id = '$del_id' ";
$result3 = $mysqli->query($sql3) or die(mysqli_error($mysqli));
if (!$result3)
{
die(mysqli_error($mysqli)) ;
}
echo "1 record added";
}
}
if(isset($_POST['delete'])) // from button name="delete"
{
$checkbox = $_POST['checkbox']; //from name="checkbox[]"
$countCheck = count($_POST['checkbox']);
for($i=0;$i<$countCheck;$i++)
{
$del_id = $checkbox[$i];
$sql = "DELETE from events where id = '$del_id' ";
$result = $mysqli->query($sql) or die(mysqli_error($mysqli));
}
}
$query = 'SELECT id, title, description FROM events WHERE evdate = "1/9/2013" order by title asc';
$result2 = $mysqli->query($query) or die(mysqli_error($mysqli));
echo '<br><br><br>';
echo '<b><div align="center"> "1/9/2013"</div></b>';
if ($result2) {
// create a new form and then put the results
// into a table.
echo "<form method='post' action=".$_SERVER['PHP_SELF'].">";
echo "<table cellspacing='0' cellpadding='3'>
<th align='left'>Interval orar</th>
<th align='left'>Eveniment</th>
<th align='left'></th>
";
while ($row = $result2->fetch_object()) {
$title = $row->title;
$description = $row->description;
$id = $row->id;
//put each record into a new table row with a checkbox
echo "
<tr>
<td align='left'><input type='text' name='title' size='20' value='$title'></td>
<td align='left'><input type='text' name='description' size='50' value='$description'></td>
<td align='left'><input type='checkbox' name='checkbox[]' id='checkbox[]' value='$id' /></td>
</tr>
";
}
// when the loop is complete, close off the list.
echo "</table>
<p>
<input id='edit' type='submit' class='button' name='edit' value='Edit'/>
<input id='delete' type='submit' class='button' name='delete' value='Delete'/>
</p>
</form>";
}
?>
</body>
</html>
id=$del_id under for loop,double check your SQL statements and variables bound to them.
gave up on the code above and went with this example from
http://bohemiawebsites.com/PHP-MYSQL-Update-Multiple-Rows.html
Everything works if anybody needs it...
I would like to apologize if the duplicate of this question exist. i tried to find and could find anything here that could solve my problem..
I am using a form to get the input and update it in the mysql database, and then retrieve the records in the html form, and have defined the code for deleting the records individually through hyperlinks. however i want to do more, i want to use the checkboxes to delete the multiple records.
my code goes like this.
<?php
//include connection string
include('connection.php');
?>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post"/>
Username : <input type="text" name="user"/><br />
Password : <input type="password" name="pass"/><br />
<input type="submit" name="submit" value="Send"/>
</form>
<?php
// query to insert into database
if(isset($_POST['user']) && isset($_POST['pass'])) {
$user = empty($_POST['user']) ? die(mysql_error()) : mysql_escape_string($_POST['user']);
$pass = empty($_POST['pass']) ? die(mysql_error()) : sha1(mysql_escape_string($_POST['pass']));
$query = "INSERT INTO users(name, pass) VALUES ('$user', '$pass')";
$result = mysql_query($query) or die(mysql_error());
}
if(isset($_GET['id'])) {
//query to delete the records
$query = "DELETE FROM users WHERE id = " . intval($_GET['id']);
$result = mysql_query($query);
}
//query to retrieve records
$query = "SELECT * FROM users";
$result = mysql_query($query);
if(mysql_num_rows($result) > 0 ) {
echo "<table cellpadding=10 border=1>";
while ($row = mysql_fetch_row($result)) {
echo "<tr>";
echo "<td>" . $row[0] . "</td>";
echo "<td>" . $row[1] . "</td>";
echo "<td>" . $row[2] . "</td>";
echo "<td>delete";
echo "</tr>";
}
echo "</table>";
}
?>
i would like you to know that i am a newbie to programming world and i am not so sure of how exactly html checkbox work and how do i use it to delete the multiple records. i want to know what extra code do i have to write for it, and i would appreciate a lot if someone explains me that extra code in brief..
thank you..
This is probably a good time for another form:
<?php
// query to insert into database ...
// ... etc...
if(isset($_POST["formDeleteSelected"])) {
//query to delete the records
$query = "DELETE FROM users WHERE id IN (" . implode(", ",$_POST["rowid"]) . ")";
$result = mysql_query($query);
header("Location: mycode.php"); // just so 'refresh' doesn't try to run delete again
exit();
}
?>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
<?php
//query to retrieve records
$query = "SELECT * FROM users";
$result = mysql_query($query);
if(mysql_num_rows($result) > 0 ) {
echo "<table cellpadding=10 border=1>";
while ($row = mysql_fetch_row($result)) {
echo "<tr>";
echo "<td><input type="checkbox" name="rowid[]" value=\"" . $row[0] . "\" /></td>";
echo "<td>" . $row[0] . "</td>";
echo "<td>" . $row[1] . "</td>";
echo "<td>" . $row[2] . "</td>";
echo "</tr>";
}
echo "</table>";
}
?>
<input type="submit" name="formDeleteSelected" text="Delete Selected" />
</form>
Or something like that (I haven't actually tried that code so there may be a typo). Also note that you should make sure to sanitize any form/get inputs for SQL Injection (plenty of information on that in other Stack Overflow questions).
First of all you need a checkbox and the id you want to delete:
<input id="delete" type="checkbox" name="delete" /><label for="delete">Delete user</label>
<input type="hidden" name="user_id" value="12345" />
You can then test if the checkbox has been set and then manually set the GET parameter to reuse your existing code:
if(isset($_POST['delete'])){
$_GET['id'] = $_POST['user_id'];
}
That's not the most elegant solution but a really simple one that should work with your code.
try an SQL query with a list of IDs
... WHERE id=$sentIds[0] OR id=$sentIds[1] OR ...
or use a set operation
... WHERE id IN ($i1,$i2 ... );
You sure have to send ids in the form for this to work, but You know that ;)