Skipping empty records in mysql with php - php

I am trying to make a survey but when I have an empty record in my msql database he just shows an empty question is there a possibility to skip empty records?
<?php
session_start();
include "config.php";
connDB();
$some_questions = mysql_num_rows(mysql_query("SELECT id FROM question"));
if($_POST['next'] && $_SESSION['qnumber'] != '' && $_POST['question'.$_SESSION['qnumber']] != '')
{
if($_POST['question1']){
$_SESSION['antwoordjes'] = $_POST['question1'];
}else{
$_SESSION['antwoordjes'] .= '|'.$_POST['question'.$_SESSION['qnumber']];
}
$_SESSION['qnumber']++;
$questionnumber = $_SESSION['qnumber']++;
$sql = "SELECT id, question FROM question WHERE id = '$questionnumber'";
$result = mysql_query($sql);
while ($count = mysql_fetch_array($result)){
if($count['question'] == null)
{
$_SESSION['qnumber']++;
}
}
}
if($_POST['question'.$some_questions] != '' && $_SESSION['qnumber'] >= ($some_questions + 1))
{
mysql_query("INSERT INTO answers (ip,answers,datum) VALUES ('".$_SERVER['REMOTE_ADDR']."','".$_SESSION['antwoordjes']."',NOW())");
session_destroy();
header("Location: outro.php");
}
if($_SESSION['qnumber'] == '' || $_SESSION['qnumber'] >= ($some_questions + 1))
{
$quest = 1;
$_SESSION['qnumber'] = $quest;
}
else{
$quest = $_SESSION['qnumber'];
}
$data = mysql_fetch_array(mysql_query("SELECT question,answers FROM question WHERE id='".$quest."'"));
$answers = explode('|',$data['answers']);
$quest = stripslashes($data['question']);
if(empty($_POST['atext']))
{
}else{
$quest = $_POST['hsvraag'];
$commentary = $_POST['atext'];
toevoegenOverig($commentary, $quest);
}
?>
<script type="text/javascript">
function disablefield(){
if (document.getElementById('yes_radio').checked == 1){
document.getElementById('textbox_A').disabled='disabled';
document.getElementById('textbox_A').value='';
}else{
document.getElementById('textbox_A').disabled='';
document.getElementById('textbox_A').value=''; }
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<!-- <script type="text/javascript" src="abc.js"></script> -->
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Internet onderzoek</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table align="left">
</table>
<div class="centrum">
<div class="header">Internetsurvey</div>
<div class="content" id="content">
<div class="balk">Introductie | <span style="color:#000033;">question</span> | end</div>
<br/>
<form method="post" action="vragenv.php">
<?php
$nummer = 1;
foreach($answers as $answer)
{
echo '<input id="yes_radio" type="radio" name="question'.$_SESSION['qnumber'].'" value="'.$nummer.'" id="answer'.$nummer.'" onChange="disablefield();"/><label for="answer'.$nummer.'">'.$answer.'</label><br />';
echo '<input type="hidden" name="hsvraag" value="'.$quest.'"/>';
$nummer++;
}
?>
</br>
<br/><input type="text" id="textbox_A" required name="atext"/><br />
<input type="submit" name="next" value="next" class="knop" />
</form>
</div>
</div>
</body>
</html>

Tweak your query.
SELECT
`id`,
`question`
FROM
`question`
WHERE
`id` = '$questionnumber'
AND
`question` IS NOT NULL
AND
LENGTH(TRIM(`question`))>1

Related

how to display same serial no on two input fields simaltaneously from database

i have two html forms on same page and on top of each form there is an input field that is intended to give each form a serial no.
i am using phpdesktop chrome with html5, php, and sqlite3 to show a 6 digit no from orders.db database in an input field
here is my code that i am using
forms.php
<?php
session_start();
class DBUtil extends SQLite3 {
function __construct() {
//Open db
$this->open("orders.db");
}
function createTableIfNotExists() {
//create table if not exists already
$sql = "CREATE TABLE IF NOT EXISTS order_sequence (" . " next_seq INT NOT NULL" . ")";
$result = $this->exec($sql);
return $result; //Whether table created successfully or not
}
function updateNextSeqNumber($nextSeq) {
//update next sequnce number in db
$sql = "UPDATE order_sequence SET next_seq = " . $nextSeq;
$result = $this->exec($sql);
return $result;
}
function insertFirstTime() {
//insert first time
$sql = "INSERT INTO order_sequence (next_seq) VALUES (1)";
$result = $this->exec($sql);
return $result;
}
function getNextSeq() {
//get next sequence number
$sql = "SELECT next_seq FROM order_sequence";
$result = $this->query($sql);
$nextSeq = 1;
if ($row = $result->fetchArray(SQLITE3_ASSOC)) {
$nextSeq = $row["next_seq"];
} else {
$this->insertFirstTime(); //First sequence number, so that next update queries will be always successfull
}
$this->updateNextSeqNumber($nextSeq + 1);
return $nextSeq;
}
}
function getNextSequnceNumber() {
//Create new object of utility class
$db = new DBUtil();
if (!$db) {
die("Connection failed: " . $db->lastErrorMsg());
}
//Check if connected
if ($db->createTableIfNotExists()) {
$nextSeq = $db->getNextSeq();
return formatToSixCharacters($nextSeq);
} else {
die("Error: " . $db->lastErrorMsg());
}
//close connection finally
$db->close();
}
function formatToSixCharacters($num) {
$numStr = $num . "";
if (strlen($numStr) < 6) {
$zeros = 6 - strlen($numStr);
for ($i = 1;$i <= $zeros;$i++) {
$numStr = "0" . $numStr;
}
}
return $numStr;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta content="text/html; charset=utf-8" http-equiv=Content-Type>
<title></title>
</head>
<body>
<!-- form-2 -->
<div class="grid">
<div class="grid-m1">
<div class="grid-c1">
<h2 class="form-no">(form-1)</h2>
<input id="division-no" class="serial-no division-no" type="text" name="fname" value="<?php echo getNextSequnceNumber() ?>" maxlength="6" size="6">
</div>
</div>
</div>
<!-- form-2 -->
<div class="grid">
<div class="grid-m1">
<div class="grid-c1">
<h2 class="form-no">(form-2)</h2>
<input id="division-no" class="serial-no division-no" type="text" name="fname" value="<?php echo getNextSequnceNumber() ?>" maxlength="6" size="6">
</div>
</div>
</div>
</body>
</html>
and this above code is working fine except one problem and the problem is that it shows 000001 in form-1 input field and then add 1 and shows 000002 in form-2 but i want it to show same serial no on both forms e.g 000001 on form-1 and also 000001 on form-2 thats it!
since i know little about php and sqlite i cann't figure it out.
any help will be really appreciated.
ps: i don't want to change the nature of the code because every time i clicked on the link to that page it increases serial no by one in the input field.
Each time you're calling getNextSequnceNumber() you are incrementing the value. You want to store the sequence number in a variable once and then use that variable in multiple places.
Also, the way your code is setup, it will increment on every page load.
...
<?php
$sequenceNumber = getNextSequnceNumber();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta content="text/html; charset=utf-8" http-equiv=Content-Type>
<title></title>
</head>
<body>
<!-- form-2 -->
<div class="grid">
<div class="grid-m1">
<div class="grid-c1">
<h2 class="form-no">(form-1)</h2>
<input id="division-no" class="serial-no division-no" type="text" name="fname" value="<?php echo $sequenceNumber; ?>" maxlength="6" size="6">
</div>
</div>
</div>
<!-- form-2 -->
<div class="grid">
<div class="grid-m1">
<div class="grid-c1">
<h2 class="form-no">(form-2)</h2>
<input id="division-no" class="serial-no division-no" type="text" name="fname" value="<?php echo $sequenceNumber; ?>" maxlength="6" size="6">
</div>
</div>
</div>
</body>
</html>
EDIT
<?php
session_start();
class DBUtil extends SQLite3
{
function __construct()
{
//Open db
$this->open("orders.db");
}
function createTableIfNotExists()
{
//create table if not exists already
$sql = "CREATE TABLE IF NOT EXISTS order_sequence (" . " next_seq INT NOT NULL" . ")";
$result = $this->exec($sql);
return $result; //Whether table created successfully or not
}
function updateNextSeqNumber($nextSeq)
{
//update next sequnce number in db
$sql = "UPDATE order_sequence SET next_seq = " . $nextSeq;
$result = $this->exec($sql);
return $result;
}
function insertFirstTime()
{
//insert first time
$sql = "INSERT INTO order_sequence (next_seq) VALUES (1)";
$result = $this->exec($sql);
return $result;
}
function getNextSeq()
{
//get next sequence number
$sql = "SELECT next_seq FROM order_sequence";
$result = $this->query($sql);
$nextSeq = 1;
if ($row = $result->fetchArray(SQLITE3_ASSOC)) {
$nextSeq = $row["next_seq"];
} else {
$this->insertFirstTime(); //First sequence number, so that next update queries will be always successfull
}
$this->updateNextSeqNumber($nextSeq + 1);
return $nextSeq;
}
}
function getNextSequnceNumber()
{
//Create new object of utility class
$db = new DBUtil();
if (!$db) {
die("Connection failed: " . $db->lastErrorMsg());
}
//Check if connected
if ($db->createTableIfNotExists()) {
$nextSeq = $db->getNextSeq();
return formatToSixCharacters($nextSeq);
} else {
die("Error: " . $db->lastErrorMsg());
}
//close connection finally
$db->close();
}
function formatToSixCharacters($num)
{
$numStr = $num . "";
if (strlen($numStr) < 6) {
$zeros = 6 - strlen($numStr);
for ($i = 1; $i <= $zeros; $i++) {
$numStr = "0" . $numStr;
}
}
return $numStr;
}
$sequenceNumber = getNextSequnceNumber();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta content="text/html; charset=utf-8" http-equiv=Content-Type>
<title></title>
</head>
<body>
<!-- form-2 -->
<div class="grid">
<div class="grid-m1">
<div class="grid-c1">
<h2 class="form-no">(form-1)</h2>
<input id="division-no" class="serial-no division-no" type="text" name="fname"
value="<?php echo $sequenceNumber; ?>" maxlength="6" size="6">
</div>
</div>
</div>
<!-- form-2 -->
<div class="grid">
<div class="grid-m1">
<div class="grid-c1">
<h2 class="form-no">(form-2)</h2>
<input id="division-no" class="serial-no division-no" type="text" name="fname"
value="<?php echo $sequenceNumber; ?>" maxlength="6" size="6">
</div>
</div>
</div>
</body>
</html>

Display result of php date range serach in same html form

I want to do a search in my database base on date range.I can get a result in mysql console:
SELECT SUM(amount) as sum from serviceces where date_of_service between '2014-11-06' and '2014-111- 12';
//so in my form
<?php
include_once('../../libs/search/search_sales_by_date.php');
include_once('../../libs/search/total_sales.php');
?>
<?php error_reporting (E_ALL ^ E_NOTICE); ?>
<!Doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>search by dates</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script>
$(function() {
$( ".datepicker" ).datepicker({ dateFormat: 'mm-dd-yy' });
});
</script>
</head>
<body>
<div>
<?php
if(isset($error)) {
echo $error;
}
if(isset($success)) {
echo $success;
}
?>
</div>
<h3>Search Sales</h3>
<p>From</p>
<form method="GET" action="search_sales_by_dates.php">
<p>Date: <input type="text" class="datepicker" name="from_date" id="field1"></p>
<p>To</p>
<p>Date: <input type="text" class="datepicker" name="to_date" id="field2"></p><br />
<input type="submit" value="search" name="submit" id="submitdata">
</form>
//search.php
<?php
//$total_by_date_range = "";
if(isset($_GET['submit'])) {
include_once('../../classes/class.ManageServices.php');
$init = new ManageServices();
$date_from = (isset($_GET['from_date']) ? $_GET['from_date'] : null);
$date_to = (isset($_GET['to_date']) ? $_GET['to_date'] : null);
if(empty($date_from) && empty($date_to)) {
$error = "No search query";
} elseif(empty($date_from) && !empty($date_to)) {
$error = "Specify your end date search";
} elseif(!empty($date_from) && empty($date_to)) {
$error ="Specify your start date search";
} else {
$total_by_date_range = 0;
$total_by_date_range = $init->getSumByDateRange($date_from, $date_to);
if($total_by_date_range== 1) {
$success ="Total calculated base on your search: ".$total_by_date_range;
} elseif (!$total_by_date_range) {
$error = "error";
} else {
$error = "Record not found due to some errors";
}
}
}
?>
//class.ManageServices.php
function getSumByDateRange($date_from, $date_to) {
$query = $this->link->query("SELECT SUM(amount) as sum_of_date_range FROM services WHERE
date_of_service between '".$date_from."' and '".$date_to."'");
$rowcount = $query->rowCount();
$result = $query->fetch();
return $result;
}
My problem is, if I do an echo $total_date_by_range in my form,it shows nothing ,doing a vardump($total_by_date_range) displays null.What's wrong with my code? Also I need to display the result on the same form. Any ideas on how to achieve this? I put at the top of form since it will display notice unidentified index.....everytime I submit the button.
Updates..
vardump($total_by_date_range) results in
array (size=1)
0 =>
array (size=2)
'sum_of_date_range' => null
0 => null
why does 'sum_of_date_range' null?

Why do I have all these errors?

In two pages I have there problem. The first problem is before I submitted I get this alert:
(Notice: Undefined index: submit in C:\xampp\htdocs\art-legend\12\admin\add.php on line 15)
and the line 15 in this page is
(if ($_POST['submit'] && !empty($_FILES)){)
The second problem is before I put any image they put that I have an image on the top but I still don't put any image and I have this picture
And the third error is when I upload an image I can't see it and I have a screen like the last screen .
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256" />
<title>Untitled Document</title>
<title>Untitled Document</title>
</head>
<body>
<?
if ($_POST['submit'] && !empty($_FILES)){
$formok = TRUE;
$title = $_POST['title'];
$thread = $_POST['elm1'];
$date = date("d/m/y h;i:s");
$path = $_FILES['upload']['tmp_name'];
$name = $_FILES['upload']['name'];
$size = $_FILES['upload']['size'];
$type = $_FILES['upload']['type'];
$error = $_FILES['upload']['error'];
if (!is_uploaded_file($path)){
$formok = FALSE;
echo "there is no file uploaded try again";
}
if (!in_array($type,array('image/png','image/jpg','image/jpeg'))){
$formok = FALSE ;
echo "the file is not image try again ";
}
if (filesize($path)>800000){
$formok = FALSE ;
echo "the file is bigger ";
}
if ($formok){
if ($connect = mysqli_connect('localhost','root','adminpass','flip')){
$content = file_get_contents($path);
$safetitle = mysqli_real_escape_string($connect,$title);
$safethread = mysqli_real_escape_string($connect,$thread);
$safeimage = mysqli_real_escape_string($connect,$thread);
$sqltitle ="insert into title(title) values ('$safetitle')";
$sqlthread = "insert into threads (topic,date) values ('$safethread','$date')";
$sqlimge ="insert into images (name,size,type,content) values ('$name','$size','$type','$safeimage')";
$querytitle = mysqli_query($connect,$sqltitle);
$querythread = mysqli_query($connect,$sqlthread);
$queryimage = mysqli_query($connect,$sqlimge);
if ($querytitle && $querythread && $queryimage){
$imageid = mysqli_insert_id($connect);
}
mysqli_close($connect);
}
else {
echo "There is error in database connect";
}
echo "insert done all ";
}
}
?>
<img src="image.php?id=<?=$imageid; ?>" />
<hr />
<form action="<?php echo $_SERVER["PHP_SELF"];?>" method="post" enctype="multipart/form-data">
<p>
title :
<input type="text" name="title" />
</p>
<div>
<textarea id="elm1" name="elm1" rows="15" cols="80" style="width: 80%">
</textarea>
</div>
<input type="hidden" name="MAX_FILE_SIZE" value="800000" />
<p>
<input type="file" name="upload" />
</p>
<p>
<input type="submit" name="submit" value="process" />
</p>
</form>
</body>
</html>
--
<?
$imageid = $_GET['id'];
if ( mysqli_connect('localhost','root','adminpass','flip')){
$escape = mysqli_real_escape_string($connect,$content);
$sql = "select type,content from images where id =$imageid";
$query = mysqli_query($connect, $sql);
$fetch = mysqli_fetch_array($query,MYSQLI_ASSOC);
mysqli_free_result($query);
mysqli_close($connect);
}
else {
echo"ther is no connection 2";
}
if (!empty($fetch)){
header("Content-type:{$fetch['type']}");
echo $fetch['content'];
}
?>
Use isset
isset — Determine if a variable is set and is not NULL
<?
if (isset($_POST['submit']) && !empty($_FILES)){

Advance to the next q in a quiz using PHP/MYSQL

I am making a quiz in php and I am having trouble advancing to the next question if they get the right answer. I want to stay on the same page. I tried adding 1 to the primary key of the question if a right answer is submitted but that didn't seem to work.
<?php
include('includes/config.inc.php');
?>
<?php
if ($_POST==true && array_key_exists('ans', $_POST)){
$ans = $_POST['ans'];
$question = $_POST['question'];
$sql=mysql_query("SELECT * FROM answer WHERE pk_answer = $ans;");
while($row=mysql_fetch_array($sql)){
$correct=$row['correct'];
}
echo $correct;
if ($correct == 1) {
echo "You are so cool!!!!";
} else {
$msg = "You submitted an incorrect answer. Please try again.";
$incorrect = $ans;
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>G.A.M.E.</title>
<style>
.incorrect {
color: #F00;
font-weight:bold;
}
</style>
</head>
<body>
<h3>ASTM SAFETY QUIZ QUESTION 1</h3>
<?php
$sql=mysql_query("SELECT * FROM quiz LIMIT 1");
while($row=mysql_fetch_array($sql)){
$pk_quiz=$row['pk_quiz'];
$fk_module=$row['fk_module'];
$fk_task=$row['fk_task'];
$quiz_benchmark=$row['quiz_benchmark'];
$question=$row['question'];
?>
<p><?php echo $question; }?></p>
<form id="quiz" name="form1" method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
<?php $sql2=mysql_query("SELECT * FROM answer ORDER BY RAND()");
while($row2=mysql_fetch_array($sql2)){
$pk_answer=$row2['pk_answer'];
$fk_quiz=$row2['fk_quiz'];
$answer=$row2['answer'];
$correct=$row2['correct'];
?>
<input type="radio" name="ans" value="<? echo $pk_answer; ?>" id="RadioGroup1_0" />
<?php if ($fk_answer = $pk_quiz) {
if ($incorrect == $pk_answer) {
echo '<span class="incorrect">'.$answer."</span>";
} else {
echo $answer;
}
}}?>
<input name="question" type="hidden" value="<?php echo $pk_quiz; ?>" />
<br /><input name="Submit" type="submit" value="submit" />
</p>
</form>
<?php if ($msg == true) { echo $msg; } ?>
</body>
</html>

Derive checkbox state from non-boolean MySQL with PHP

Currently I have a form that makes use of fields and check boxes. The fields and checkboxes both update the database perfectly:
<?php
function renderForm($articletitle, $articleorganization, $articledate, $articleurl, $articletags )
{
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="stylized" class="myform">
<form id="form" name="form" action="" method="post">
. . .
. . .
if(count($articletags) > 0)
{
$articletags_string = implode(",", $articletags);
}
if (isset($_POST['submit']))
{
$articletitle = mysql_real_escape_string(htmlspecialchars($_POST['articletitle']));
$articleorganization = mysql_real_escape_string(htmlspecialchars($_POST['articleorganization']));
$articledate = mysql_real_escape_string(htmlspecialchars($_POST['articledate']));
$articleurl = mysql_real_escape_string(htmlspecialchars($_POST['articleurl']));
$articletags = implode(',', $_POST['articletags']);
. . .
mysql_query("INSERT articles SET articletitle='$articletitle', articleorganization='$articleorganization', articledate='$articledate', articleurl='$articleurl', articletags='$articletags' ")
or die(mysql_error());
// once saved, redirect to success page
header("Location:addsuccess.html");
}
}
else
{
renderForm('','','','');
}
?>
Now, though, I'm wondering if I should have gone with a boolean checkbox instead.
The reason is that I've built an edit form as well and it follows the new entry form exactly except that values are already filled in via the MySQL DB.
So, I'm assuming that it would be considerably easier to use boolean, right?
So, instead of using an array, I should give the checkboxes different names, and on the edit.php page I can use something like:
<?php
function renderForm($id, $articletitle, $articleorganization, $articledate, $articleurl, $articletags)
{
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="stylized" class="myform">
<form id="form" name="form" action="" method="post">
<input type="hidden" name="id" value="<?php echo $id; ?>"/>
<h1>Edit Details for <?php echo $articletitle; ?></h1>
<fieldset>
<legend>Article details</legend>
<div class="row">
<div class="field"><label>Article Title</label><input type="text" name="articletitle" value="<?php echo $articletitle; ?>"/></div>
</div>
<div class="row">
<div class="field"><label>Article Author </label><input type="text" name="articleorganization" value="<?php echo $articleorganization; ?>"/></div>
<div class="field"><label>Article Date </label><input type="text" name="articledate" value="<?php echo $articledate; ?>"/></div>
</div>
<div class="row">
<div class="field"><label>Article Url: </label><input type="text" name="articleurl" value="<?php echo $articleurl; ?>"/></div>
<div class="row">
<input type="checkbox" name="articletags1" value="checkbox" id="articletags_0" />
<input type="checkbox" name="articletags2" value="checkbox 2" id="articletags_1" />
</div>
</fieldset>
<footer><input type="submit" name="submit" value="Submit"></footer></form>
</div>
</body>
</html>
<?php
}
include('settings.php');
if (isset($_POST['submit']))
{
if (is_numeric($_POST['id']))
{
$id = $_POST['id'];
$articletitle = mysql_real_escape_string(htmlspecialchars($_POST['articletitle']));
$articleorganization = mysql_real_escape_string(htmlspecialchars($_POST['articleorganization']));
$articledate = mysql_real_escape_string(htmlspecialchars($_POST['articledate']));
$articleurl = mysql_real_escape_string(htmlspecialchars($_POST['articleurl']));
$articletags = implode(',', $_POST['articletags']);
if ($articletitle == '' || $articletags == '')
{
$error = 'ERROR: Please fill in all required fields!';
renderForm($id, $articletitle, $articletags);
}
else
{
mysql_query("UPDATE articles SET articletitle='$articletitle', articleorganization='$articleorganization', articledate='$articledate', articleurl='$articleurl', articletags='$articletags' WHERE id=$id")
or die(mysql_error());
header("Location: editsuccess.html");
}
}
else
{
echo 'Error!';
}
}
else
{
if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0)
{
$id = $_GET['id'];
$result = mysql_query("SELECT * FROM articles WHERE id=$id")
or die(mysql_error());
$row = mysql_fetch_array($result);
if($row)
{
$articletitle = $row['articletitle'];
$articleorganization = $row['articleorganization'];
$articledate = $row['articledate'];
$articleurl = $row['articleurl'];
$articletags = $row['articletags'];
renderForm($id, $articletitle, $articleorganization, $articledate, $articleurl, $articletags, '');
}
else
{
echo "No results!";
}
}
else
{
echo 'Error!';
}
}
?>
The problem with that though is that my checkboxes on the edit.php page still aren't showing the checked state.
You need to use checked="checked" in the same way as you used for value. See the solution bottom:
<input type="checkbox" <?php if(isset($_POST[articletags1])) echo 'checked="checked" ' ?>name="articletags1" value="checkbox" id="articletags_0" />
<input type="checkbox" <?php if(isset($_POST[articletags2])) echo 'checked="checked" ' ?>name="articletags2" value="checkbox 2" id="articletags_1" />
Hope this works. Updated the right code. Use ini_set('display_errors', 1); to get the error generated.

Categories