how to make ascending id in php - php

this code not asceding id. we have id 1, 2, 3, 15. when adding next record is read id 16. so how to solve this problem ? id must 1,2,3,4,5,etc
<?php
include"class_koneksi.php";
$db=new database();
$db->konek();
if(empty($_GET['aksi'])){
$daftar=$db->tampilData();
echo"<table border=1>
<tr>
<td>Id</td>
<td>Nama</td>
<td>Email</td>
<td>Pesan</td>
<td>Edit</td>
<td>Hapus</td>
</tr>";
foreach($daftar as $data){
echo" <tr>
<td>".$data['id']."</td>
<td>".$data['nama']."</td>
<td>".$data['email']."</td>
<td>".$data['pesan']."</td>
<td><a href='?aksi=edit&id=$data[id]'>edit</a></td>
<td><a href='?aksi=hapus_data&id=$data[id]'>Hapus</a></td>
</tr>";
}
echo"</table> <br> <a href='?aksi=tambah'>TAMBAH</a>";
}elseif(isset($_GET['aksi']) && $_GET['aksi']=='tambah'){
echo"<br>
<form method=POST action='?aksi=tambah_data'>
<table>
<tr><td>Nama</td><td><input type=text name='nama'></td></tr>
<tr><td>Email</td><td><input type=text name='email'></td></tr>
<tr><td>Pesan</td><td><input type=text name='pesan'></td></tr>
<tr><td></td><td><input type=submit value='simpan'></td></tr>
</table>
</form>
";
}elseif(isset($_GET['aksi']) && $_GET['aksi']=='tambah_data'){
$id= isset($_POST['id']);
$nama=$_POST['nama'];
$email=$_POST['email'];
$pesan=$_POST['pesan'];
$db->tambahData($id,$nama,$email,$pesan);
}elseif(isset($_GET['aksi']) && $_GET['aksi']=='edit'){
$id=$_GET['id'];
echo"<br>
<form method=POST action='?aksi=update_data'>
<table>
<tr><td>Id</td><td><input type=text name='id' value='".$db->bacaData('id',$id)."'></td></tr>
<tr><td>Nama</td><td><input type=text name='nama' value='".$db->bacaData('nama',$id)."'></td></tr>
<tr><td>Email</td><td><input type=text name='email' value='".$db->bacaData('email',$id)."'></td></tr>
<tr><td>Pesan</td><td><input type=text name='pesan' value='".$db->bacaData('pesan',$id)."'></td></tr>
<tr><td></td><td><input type=submit value='simpan'></td></tr>
</table>
<input type='hidden' name='id' value='".$db->bacaData('id',$id)."'>
</form>
";
}elseif(isset($_GET['aksi']) && $_GET['aksi']=='update_data'){
$id=$_POST['id'];
$nama=$_POST['nama'];
$email=$_POST['email'];
$pesan=$_POST['pesan'];
$db->updateData($id,$nama,$email,$pesan);
}elseif(isset($_GET['aksi']) && $_GET['aksi']=='hapus_data'){
$id=$_GET['id'];
$db->hapusData($id);
}
?>
can you tell me where this code wrong?
can you help me solve this problem? thanks

Remove the auto increment property of the table which is showing 16.
Before inserting a new row in that table write a select query.
$sql = "select * from `table_name`";
$result = mysql_query($sql);
$id=count($result)+1;
if(count($result)>0){
$temp = 1;
while($row = mysql_fetch_assoc($result)){
if($row['id']!=$temp++){
$id=$temp; break;
}
}
}
$sql = "insert into `table_name` (`id`,`..`,`..`) values('"$id.."','..','..')";
mysql_query($sql);
I hope this will solve your query...

It is showing id as 16 becouse in table properties Id is set as auto increment

First remove auto increment from id in the respective database table you are having this issue. Then Replace this code section with your code.
.
.
.
elseif(isset($_GET['aksi']) && $_GET['aksi']=='tambah'){
$daftar=$db->tampilData();
$id=count($daftar)+1;
if(count($daftar)>0){
$temp = 1;
forech($daftar as $row){
if($row['id']!=$temp++){
$id=$temp; break;
}
}
}
echo"<br>
<form method=POST action='?aksi=tambah_data'>
<table>
<tr><td>Nama</td><td><input type=text name='nama'><input type="hidden" name='id' value="<?php echo $id ?>"></td></tr>
<tr><td>Email</td><td><input type=text name='email'></td></tr>
<tr><td>Pesan</td><td><input type=text name='pesan'></td></tr>
<tr><td></td><td><input type=submit value='simpan'></td></tr>
</table>
</form>
";
}elseif(isset($_GET['aksi']) && $_GET['aksi']=='tambah_data'){
$id= $_POST['id'];
$nama=$_POST['nama'];
$email=$_POST['email'];
$pesan=$_POST['pesan'];
$db->tambahData($id,$nama,$email,$pesan);
}
.
.
.
I hope this solves your requirement..

Related

Refuses to get data

I am using this code to get the value of checked box in PHP but it doesn't work in the second code. The first one is just a list for testing. The second one will echo data from a database:
When applying the first code on the test list I will get the value of the box I checked but when applying it on the second part (one that gets data from database it returns Empty)
Get the checked boxes:
<?php
if(isset($_POST['DeleteCon']) )
{
if(!empty($_POST['lang']))
{
foreach($_POST['lang'] as $value)
{
echo "value : ".$value.'<br/>';
}
}
else
{
echo "value : Empty <br/>";
}
}
?>
Test list: it works on this section of the code:
<form method="post">
<?php
echo "<span>Select languages</span><br/>
<input type='checkbox' name='lang[]' value='PHP' class='table-row'> PHP <br/>
<input type='checkbox' name='lang[]' value='JavaScript'> JavaScript <br/>
<input type='checkbox' name='lang[]' value='jQuery'> jQuery <br/>
<input type='checkbox' name='lang[]' value='Angular JS'> Angular JS <br/>"
?>
</form>
But doesn't work on this one:
<form method="post">
<?php
include('database.php');
$sql = "SELECT id, ContactID ,FirstName, LastName, Phone FROM Contact WHERE ID='1'";
$result = $conn->query($sql);
if ( !empty($result->num_rows) && $result->num_rows > 0) { // ...
// output data of each row
echo "<form method='post'>";
while($row = $result->fetch_assoc()) {
echo "<tr>
<td id='delete'>
<input type='checkbox' name='lang[]' value='PHP' class='table-row'>
</td>
<td>". $row["FirstName"]. "</td>
<td>". $row["LastName"]. "</td>
<td>". $row["Phone"] ."</td></tr>";
}
echo "</form>";
} else {
echo "<tr>
<td id='delete'>
<input type='checkbox' id='row1' class='table-row'>
</td>
<td> 0 results </tr>";
}
$conn->close();
?>
</form>

Two different radio button with one form and post it using php

I am stuck with this code, trying to post 2 different radio group my problem is I want to make the user to check 1 radio button for ervery 4 results , I mean how can I know when user has choose other, with this code I always can choose more than one radio , any help?
$sql = "SELECT * FROM tbl1 WHERE object = '".$obj."' LIMIT 5 ";
$result2 = mysqli_query($conn,$sql);
echo "<form action='' method='post'>";
while ($row2 = mysqli_fetch_array($result2,MYSQLI_ASSOC)) {
echo "<div align='center'>
<table class='demo' dir='rtl'>
<tbody>
<tr>
<td>".$row2['ojs']."</td>
<td> </td>
</tr>
<tr>
<td>".$row2['txt1']."</td>
<td><input type='radio' name='chk1' value='1' >
</tr>
<tr>
<td>".$row2['txt2']."</td>
<td><input type='radio' name='chk2' value='2' >
</tr>
<tr>
<td>".$row2['txt3']."</td>
<td><input type='radio' name='chk3' value='3' >
</tr>
<tr>
<td>".$row2['txt4']."</td>
<td><input type='radio' name='chk4' value='4' >
</tr>
</tbody>
</table><br>
</div>
";
}
echo "<input type='submit' name='submit'>
</form>";
if (isset($_POST['submit'])) {
$chk1 = $_POST['chk1'];
$chk2 = $_POST['chk2'];
$chk3 = $_POST['chk3'];
$chk4 = $_POST['chk4'];
echo $chk4."|".$chk3."|".$chk2."|".$chk1;
//insert into mysql , just the Selected radio button for each different question
$insQry = "insert into tbl2 (id,sel1,sel2,sel3,sel4) VALUES('$chk1','$chk2','$chk3','$chk4')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
You need to set the name of the entire adio input group to one name. Since you have them set to four different names, it treats them as as four different entities.
You need to give them the same name then get it in PHP like
$sql = "SELECT * FROM tbl1 WHERE object = '".$obj."' LIMIT 5 ";
$result2 = mysqli_query($conn,$sql);
echo "<form action='' method='post'>";
while ($row2 = mysqli_fetch_array($result2,MYSQLI_ASSOC)) {
echo "<div align='center'>
<table class='demo' dir='rtl'>
<tbody>
<tr>
<td>".$row2['ojs']."</td>
<td> </td>
</tr>
<tr>
<td>".$row2['txt1']."</td>
<td><input type='radio' name='chk' value='1' >
</tr>
<tr>
<td>".$row2['txt2']."</td>
<td><input type='radio' name='chk' value='2' >
</tr>
<tr>
<td>".$row2['txt3']."</td>
<td><input type='radio' name='chk' value='3' >
</tr>
<tr>
<td>".$row2['txt4']."</td>
<td><input type='radio' name='chk' value='4' >
</tr>
</tbody>
</table><br>
</div>
";
}
echo "<input type='submit' name='submit'>
</form>";
if (isset($_POST['submit'])) {
$chk = $_POST['chk'];
echo $chk;
Then simply set the adjacent one in the database to selected number and others to 0

php html return only one set of data records when clicking the view button

I need my system to show only the set of 'minutes' a user clicks on and not every set of minutes that the current user is involved in.
At the minute when I click the 'image' to view a set of minutes it prints out each set of minutes that user is involved in, I need it to only print out and be able to edit that one specific set of minutes... the edit function works but I just need it to print out what the user clicks on and not every set..PLEASE Can ANYONE help with this I have spent all day and don't understand how to get this to work.
Below is the code that shows the php and query in the minutes.php page, when you click the image for 'view' it takes you to the 'viewstudentminutes.php' page, I only want it to print out the minutes you click on not every set.
<?php
session_start();
if (!(isset($_SESSION["sess_username"]))) header ("Location: index.php");
$currentUser=$_SESSION["sess_username"];
$dbQuery = $db->prepare("select * from minute where '$currentUser'=B_number ");
$dbQuery->execute();
$numMinutes = $dbQuery->rowCount();
echo "<p>There are $numMinutes sets of minutes in the system</p>";
$oddRow=true;
while ($dbRow = $dbQuery->fetch(PDO::FETCH_ASSOC)) {
$ID = $dbRow['ID'];
$B_number = $dbRow['B_number'];
$Date = $dbRow['Date'];
$Time = $dbRow['Time'];
$Discussion = $dbRow['Discussion'];
$Actions = $dbRow['Actions'];
$Dateofnextmeeting = $dbRow['Dateofnextmeeting'];
$Status = $dbRow['Status'];
$E_number = $dbRow['E_number'];
$Supervisor_comments = $dbRow['Supervisor_comments'];
if ($oddRow) $rowClass="odd"; else $rowClass="even";
$oddRow=!$oddRow;
if (isset($_POST['editMinuteID']) && $_POST['editMinuteID']==$ID) {
echo "<tr class='$rowClass '>
<form style='display:inline' method='post' action='minutes.php'>
<input type='hidden' name='editCommitID' value='$ID'>
<td><input type='varchar' name='editB_number' value='$B_number'></td>
<td><input type='date' name='editDate' value='$Date'></td>
<td><input type='time' name='editTime' value='$Time'></td>
<td><input type='text' name='editDiscussion' value='$Discussion'></td>
<td><input type='text' name='editActions' value='$Actions'> </td>
<td><input type='date' name='editDateofnextmeeting' value='$Dateofnextmeeting'></td>
<td><input type='enum' name='editStatus' value='$Status'> </td>
<td><input type='varchar' name='editE_number' value='$E_number'></td>
<td><input type='text' name='editSupervisor_comments' value='$Supervisor_comments'></td>
<td colspan='2'><input type='image' src='edit.png'>
</form>
</tr>";
} else {
echo "<tr class='$rowClass'><td>$B_number</td><td>$Date</td> <td>$Time</td><td>$Discussion</td><td>$Actions</td><td>$Dateofnextmeeting</td> <td>$Status</td><td>$E_number</td><td>$Supervisor_comments</td>
echo "<tr class='$rowClass'><td>$B_number</td><td>$Date</td><td>$Time</td><td>$Discussion</td><td>$Actions</td><td>$Dateofnextmeeting</td><td>$Status</td><td>$E_number</td><td>$Supervisor_comments</td>
<td class='operation'>
<form class='display:inline' method='post' action='viewstudentminutes.php'>
<input type='hidden' name='editMinute_ID' value='$ID'>
<input type='image' src='edit.png' style='padding-top:7px'>
</form></td>
<td class='operation'>
<form class='display:inline' method='post' action='minutes.php'
onsubmit='return confirm(\"Are you sure?\")'>
<input type='hidden' name='deleteMinuteID' value='$ID'>
<input type='image' src='delete.png' style='padding-top:7px'>
</form></td>
<td class='operation'>
<form class='display:inline' method='post' action='viewstudentminutes.php'
onsubmit='viewstudentminutes.php'>
<input type='image' src='view.png' style='padding-top:7px'>
</form></td>
</tr>";
}
}
?>
Below is the code for the 'viewstudentminutes.php' when trying to edit the set of minutes
<h2>Minutes</h2>
<table class="table table-condensed">
<?php
$dbQuery= $db->prepare("SELECT * FROM `Minute` WHERE `B_number` = '" . $currentUser . "' AND `id` = " . $_POST['Minute_ID']);
$dbQuery->execute();
$oddRow=true;
while ($dbRow = $dbQuery->fetch(PDO::FETCH_ASSOC)) {
$ID = $dbRow['ID'];
$B_number = $dbRow['B_number'];
$Date = $dbRow['Date'];
$Time = $dbRow['Time'];
$Discussion = $dbRow['Discussion'];
$Actions = $dbRow['Actions'];
$Dateofnextmeeting = $dbRow['Dateofnextmeeting'];
$Status = $dbRow['Status'];
$E_number = $dbRow['E_number'];
$Supervisor_comments = $dbRow['Supervisor_comments'];
if ($oddRow) $rowClass="odd"; else $rowClass="even";
$oddRow=!$oddRow;
if (isset($_POST['editMinuteID']) && $_POST['editMinuteID']==$id) {
echo "<tr class='$rowClass'>
<form style='display:inline' method='post' action='viewstudentminutes.php'>
<input type='hidden' name='editCommitID' value='$ID'>
<tr><td colspan='2'><input type='image' src='edit.png'> </tr>
<tr><th>BNumber</th><td><input type='varchar' name='editB_number' value='$B_number'></td></tr>
<tr><th>Date</th><td><input type='date' name='editDate' value='$Date'></td></tr>
<tr><th>Time</th><td><input type='time' name='editTime' value='$Time'></td></tr>
<tr><th>Discussion</th><td><input type='text' name='editDiscussion' value='$Discussion'></td></tr>
<tr><th>Actions</th><td><input type='text' name='editActions' value='$Actions'></td></tr>
<tr><th>Date of Next Meeting</th><td><input type='date' name='editDateofnextmeeting' value='$Dateofnextmeeting'></td></tr>
<tr><th>Status</th><td><input type='enum' name='editStatus' value='$Status'></td></tr>
<tr><th>E Number</th><td><input type='varchar' name='editE_number' value='$E_number'></td></tr>
<tr><th>Supervisor Comments</th><td><input type='text' name='editSupervisor_comments' value='$Supervisor_comments'></td></tr>
</form>
</tr>";
} else {
echo "<tr class='$rowClass'>
<td class='operation'>
<form class='display:inline' method='post' action='viewstudentminutes.php'>
<input type='hidden' name='editMinute_ID' value='$ID'>
<input type='image' src='edit.png' style='padding-top:7px'>
</form></td>
<tr><th>BNumber</th><td>$B_number</td></tr>
<tr><th>Date</th><td>$Date</td></tr>
<tr><th>Time</th><td>$Time</td></tr>
<tr><th>Discussion</th><td>$Discussion</td></tr>
<tr><th>Actions</th><td>$Actions</td></tr>
<tr><th>Date of Next Meeting</th><td>$Dateofnextmeeting</td></tr>
<tr><th>Status</th><td>$Status</td></tr>
<tr><th>E Number</th><td>$E_number</td></tr>
<tr><th>Supervisor Comments</th><td>$Supervisor_comments</td></tr>
</tr>";
}
}
?>
</table>
The Minute table within the database holds the following fields...
1 ID Primary int(10) AUTO_INCREMENT
2 B_number varchar(15) latin1_swedish_ci
3 Date date
4 Time time
5 Discussion text latin1_swedish_ci
6 Actions text latin1_swedish_ci
7 Dateofnextmeeting date
8 Status enum('Submitted', 'Approved', 'Rejected', '') latin1_swedish_ci
9 Supervisor_comments text latin1_swedish_ci
10 E_number varchar(15) latin1_swedish_ci
Please help :(
There are a few variables that I'm unsure of what they are - updating your question to include what $currentUser is would help.
That said, the issue may be in your SQL statement:
$db->prepare("select * from minute where '$currentUser'=B_number ");
You are selecting where $currentUser is equal to B_number. But in your database schema, you show that B_number is a column.
I'd try this SQL statement instead:
$db->prepare("SELECT * FROM `Minute` WHERE `B_number` = '" . $currentUser . "' AND `id` = " . $yourIDVariable );
Edit
Your viewstudentminutes.php will iterate over every record returned in your SQL query. You need to uniquely identify your Minute record to show just one on your viewstudentminutes.php page. You access this page via clicking on an <img> tag. But you never send data for which Minute object you want with this form. You could add this via:
<form class='display:inline' method='post' action='viewstudentminutes.php' onsubmit='viewstudentminutes.php'>
<input type='image' src='view.png' style='padding-top:7px'>
<input type='hidden' name='minute_id' value='" . $ID . "'>
</form>
Then access this on your viewstudentminutes.php as
$db->prepare("SELECT * FROM `Minute` WHERE `B_number` = '" . $currentUser . "' AND `id` = " . $_POST['minute_id']);

Why won't my variables pass along each page? Php

I can't get my variable "quiz_name" passed along several pages in order to make a quiz. After the "quiz_name" variable is sent from question_menu.php as a $_POST[] to quiz_created.php for processing the quiz_created.php is suppose to send the variable back to question_menu.php. What am I doing wrong. To be honest I think I'm approaching it wrong.
Reworded:
quiz_name = "How to score a Basket" which is submitted to question_menu.php as a $_POST['quiz_name']. Then is submitted a to quiz_created.php as a $_POST['quiz_name'] and then submitted back to question_menu.php.
I get an Variable quiz_name undefined error after it is sent back to quesion_menu.
coach_quizzes.php
<head>
<title>Your Quizzes</title>
</head>
<body>
<h1> Current Quizzes </h1>
<form name="submit_button" action="create-quiz.php">
<input type="submit" value="Create Quiz">
</form>
</body>
</html>
submit to -> create_quiz.php
<?php session_start()
?>
<head>
<title>Your Quizzes</title>
</head>
<body>
<h1> Enter Quiz Name </h1>
<form action="questions_menu(test).php" method="post">
<input type="text" name="quiz_name" maxlength="30" size="30">
<input type="submit" value="Create Quiz">
</form>
</body>
</html>
Enter: "How to score a Basket" then submit to question_menu.php
<!DOCTYPE html>
<html>
<head>
<title>Add Question</title>
<link rel="stylesheet" type="text/css" href="css/info_style.css" />
</head>
<body>
<div id="main">
<header>
<div id="welcome">
<h2>Prairie View A&M University</h2>
</div><!--close welcome-->
</header>
<div id="site_content">
<form enctype="multipart/form-data" method="post" action="quiz_created.php">
<table border="0" name"form_table">
<tr>
<td>Quiz Name</td>
<td><?php echo $_POST['quiz_name']?></td>
</tr>
<tr>
<td>Question</td>
<td><textarea name="description" rows="4" cols="50"></textarea></td>
</tr>
<tr>
<td>Option 1</td>
<td><input type="text" name="option1" maxlength="30" size="30"></td>
</tr>
<tr>
<td>Option 2</td>
<td> <input type="text" name="option2" maxlength="30" size="30"></td>
</tr>
<tr>
<td>Option 3</td>
<td><input type="text" name="option3" maxlength="30" size="30"></td>
</tr>
<tr>
<td>Answer</td>
<td>
<select name="dropdown">
<option value='option1'>Option 1</option>
<option value='option2'>Option 2</option>
<option value='option3'>Option 3</option>
</select>
</td>
</tr>
<tr>
<td>Image</td>
<td><input type="file" name="file" /><br />
<input type="hidden" name="MAX_FILE_SIZE" value="10000" /><br>
</td>
</tr>
<tr>
<td colspan="2"><p>
<input type="submit" value="Add Question">
</p></td>
</tr>
</table>
</form>
<?php
$username = "root";
$password = "";
$hostname = "localhost";
$database = "basketball_database";
$table = "coach_john";
$con = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MYsql");
//echo "Connected to mysql<br>";
mysql_select_db("$database")
or die("Could not select Basketball_database");
//echo "Connected to database";
//update when update button pressed
if(isset($_POST['update'])){
$UpdateQuery = "UPDATE $table SET question_description='$_POST[description]', option_a='$_POST[option1]', option_b='$_POST[option2]', option_c='$_POST[option3]', answer='$_POST[dropdown]', question_id='$_POST[questionID]' WHERE question_id='$_POST[hidden]'";
mysql_query($UpdateQuery, $con);
};//end of if statement
//delete when delete button pressed
if(isset($_POST['delete'])){
$DeleteQuery = "DELETE FROM $table WHERE question_id='$_POST[hidden]'";
mysql_query($DeleteQuery, $con);
};//end of if statement
$mysql = "SELECT * FROM $table";
$mydata = mysql_query($mysql,$con);
//create table
echo "<table border=1
<tr>
<th>Question ID</th>
<th>Quiz Name</th>
<th>Question Description</th>
<th>Option 1</th>
<th>Option 2</th>
<th>Option 3</th>
<th>Answer</th>
<th>Picture</th>
</tr>";
//insert data into rows
while($records = mysql_fetch_array($mydata)){
echo "<form action=questions_menu(test).php method=post>";
echo "<tr>";
echo "<td>"."<input type=text name=questionID size=5 value=".$records['question_id']." </td>";
echo "<td>"."<input type=text name=option1 size=18 value=".$records['quiz_name']." </td>";
echo "<td>"."<textarea name=description rows=1 cols=25>".$records['question_description']."</textarea>"."</td>";
echo "<td>"."<input type=text name=option1 size=18 value=".$records['option_a']." </td>";
echo "<td>"."<input type=text name=option2 size=15 value=".$records['option_b']." </td>";
echo "<td>"."<input type=text name=option3 size= 15 value=".$records['option_c']." </td>";
echo "<td>"."<input type=text name=answer size=15 value=".$records['answer']." </td>";
echo "<td>". $records['image'] ." </td>";
echo "<td>"."<input type=hidden name=hidden value=".$records['question_id']." </td>";
//update button
echo "<td>"."<input type=submit name=update value=Update onclick='return confirm(\"Are you sure you want to update question?\")'>"." </td>";
//delete button
echo "<td>"."<input type=submit name=delete value=Delete onclick='return confirm(\"Are you sure you want to delete question?\")'>"." </td>";
echo "</tr>";
echo "</form>";//end form
} echo "</table>";
mysql_close();
?> <!-- End of php code-->
</div><!--close site_content-->
<footer>
Home | Photos | Videos | Schedule | Contact<br/><br/>
</footer>
</div><!--close main-->
</body>
</html>
after the information is added submit value= "Add Question" information is sent to quiz_created.php:
<?php
$username = "root";
$password = "";
$hostname = "localhost";
$database = "basketball_database";
$table = "coach_john";
$con = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MYsql");
// echo "Connected to mysql<br>";
$db = mysql_select_db("$database")
or die("Could not select Basketball_database");
//echo "Connected to database";
$mysql = "INSERT INTO $table(question_description, quiz_name, option_a, option_b, option_c, answer) VALUES('$_POST[description]','$_POST[quiz_name]','$_POST[option1]','$_POST[option2]','$_POST[option3]','$_POST[dropdown]')";
if(!mysql_query($mysql))
echo mysql_errno($con) . ": " . mysql_error($con) . "\n";
//die("Disconnected");
$quiz=$_POST['quiz_name'];
//Upload images
if($_POST)
{
if ($_FILES["file"]["error"] > 0)
{
// if there is error in file uploading
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
// check if file already exit in "images" folder.
if (file_exists("images/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{ //move_uploaded_file function will upload your image.
if(move_uploaded_file($_FILES["file"]["tmp_name"],"uploaded/" . $_FILES["file"]["name"]))
{
// If file has uploaded successfully, store its name in data base
$image_loc=addslashes (getcwd()."/uploaded"."/".$_FILES["file"]["name"]);
$query_image = "insert into $table (image, img_location) values ('".$_FILES['file']['name']."', '$image_loc')";
if(mysql_query($query_image))
{
echo "Stored in: " . "uploaded/" . $_FILES["file"]["name"];
}
else
{
echo 'File name not stored in database';
}
}
}
}
}
//end of image upload
mysql_close();
?>
<html>
<title>User Added</title>
<body>
<h2>
Question has been added!
</h2>
</body>
<form action="questions_menu(test).php" method="post">
<input type="submit" value="Add Another Question">
<input type="hidden" name="quiz_name" value='<?php echo $quiz;?>'>
</form>
</html>
then finally sent back to question_menu.php but I get an Variable quiz_name undefined error.
I hope I can get help. Thank You.
I see many problems, but one is that you're not escaping your php inserts properly.
echo "<td>"."<input type=text name=questionID size=5 value=".$records['question_id']." </td>";
should be:
echo '<td><input type="text" name="questionID" size="5" value="'.$records['question_id'].'" /></td>';
The way you have it now, you are using the quotes for the echo and there are none for the value="". This is seen all throughout your code. Not sure where you are getting your $records array either, but that's for you to figure out.
These lines are problematic too.
$UpdateQuery = "UPDATE $table SET question_description='$_POST[description]', option_a='$_POST[option1]', option_b='$_POST[option2]', option_c='$_POST[option3]', answer='$_POST[dropdown]', question_id='$_POST[questionID]' WHERE question_id='$_POST[hidden]'";
One major problem is you're trying to set raw POST data into a database. That is a HUGE security flaw. Other problems, like the rest of your php, have to do with quotes and double quotes, semicolons, etc... Read up on syntax. Your whole script is basically wrong.

PHP buttons to delete a row in MYSQL table output

My goal is to display the entire table e.g SELECT * FROM athlete and include button for each row in the table output, that when pressed will remove that row from the database.
I have followed another post and shown below is what I have so far but does not work:
//if(isset($_POST['delete_id']) && (!empty($_POST['delete_id'])))
if(isset($_POST['Delete'])){
$delete_id = mysql_real_escape_string($_POST['delete_id']);
echo("deletebutton" . $delete_id);
//mysql_query("DELETE FROM KeepScores WHERE `id`=".$delete_id);
//header('Location: main.php');
}
else
{
$query = 'SELECT * FROM athlete';
$result = mysql_query($query);
$self = $_SERVER['PHP_SELF'];
$self = htmlentities($_SERVER['PHP_SELF']); // strip tags to improve security
echo("<table>
<tr>
<td> id </td>
<td>last name</td>
<td>first name</td>
<td>delete data</td>
</tr>");
while($row = mysql_fetch_array($result))
{
$id = $row['id'];
echo("<tr>");
echo("<td>" . $row['id'] . "</td>");
echo("<td>" . $row['lastName'] . "</td>");
echo("<td>" . $row['firstName'] . "</td>");
echo("<td> <form action='$self' method='post'>
<input type='hidden' name='delete_id value='$id' />
<input type='submit' value='Delete' />
</form>
</td>
</tr>");
}
mysql_free_result($result);
echo("</table>");
}
However this does not work. Does anyone know why. NOTE: I have left my login credentials out but I am connecting to the database elsewhere in the file.
The most probable cause is that your checking for $_POST['Delete'] having not given any of the submit buttons the attribute 'name' of 'Delete'. However, the line you have commented out above should work?
I think you should look into this areas
//if(isset($_POST['delete_id']) && (!empty($_POST['delete_id'])))
if(isset($_POST['Delete'])){
and
<input type='submit' value='Delete' />
You should include: <input type='submit' name="Delete" value='Delete' />
That should solve the problem.

Categories