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)){
Related
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>
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.
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
Thanks in advance.
I am trying to update information in my db but am having no luck and no error messages.
Here is the page code (the pid or post_id is in the URL):
<?php
include('page with functions.php');
if (isset($_GET['pid'], $_POST['title'], $_POST['body'])){
if (edit_post($_GET['pid'], $_POST['title'], $_POST['body'])){
header("Location: blog_edit.php?pid={$_GET['pid']}");
}else{
header("Location: some location.php");
}
die();
}
?>
<!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>Blog Edit</title>
</head>
<body>
<div>
<?php
if (isset($_GET['pid']) ===false || valid_pid($_GET['pid']) === false){
echo 'Invalid post ID';
}else{
$post = get_post($_GET['pid']);
?>
<h2><?php echo $post['title']; ?></h2>
<hr />
<p><?php echo $post['body']; ?></p>
<hr />
<form action="" method="post">
<p>
<label for="title">Title</label>
<input type="text" name="title" id="title" value="<?php echo $post['title']; ?>"/>
</p>
<p>
<textarea name="body" rows="20" cols="60"><?php echo $post['body']; ?></textarea>
</p>
<p>
<input type="submit" value="Edit Post" />
</p>
</form>
<?php
}
?>
</div>
</body>
</html>
Here is the function (the pid or post_id is in the URL):
// edits a blog entry
function edit_post($title, $body){
$title = mysql_real_escape_string(htmlentities($title));
$body = mysql_real_escape_string(nl2br(htmlentities($body)));
mysql_query("UPDATE `posts` (`post_title`, `post_body`) SET `post_title` = '{$title}' AND `post_body` = '{body}' WHERE `post_id` = {$pid}");
}
Any help would be great thanks!
FYI I am new to PHP MYSQL so please be kind.
if (edit_post($_GET['pid'], $_POST['title'], $_POST['body'])){
header("Location: blog_edit.php?pid={$_GET['pid']}");
}else{
header("Location: some location.php");
}
You're passing three arguments in the function call, but the function definition has only 2 arguments.
function edit_post($title, $body){
$title = mysql_real_escape_string(htmlentities($title));
$body = mysql_real_escape_string(nl2br(htmlentities($body)));
mysql_query("UPDATE `posts` (`post_title`, `post_body`) SET `post_title` = '{$title}' AND `post_body` = '{body}' WHERE `post_id` = {$pid}");
}
Also, return a boolean from the function.
function edit_post($pid, $title, $body){
$title = mysql_real_escape_string(htmlentities($title));
$body = mysql_real_escape_string(nl2br(htmlentities($body)));
$result = mysql_query("UPDATE `posts` (`post_title`, `post_body`) SET `post_title` = '{$title}' AND `post_body` = '{body}' WHERE `post_id` = {$pid}");
echo $result;
}
I'm trying to pass a value from a select input control on an HTML form.
When I hardcode it, it gets echoed, when not, all I get is this:
The invention type did not go through correctly.
Here is my page1.php:
<?php
session_start();
$_SESSION['invtype'] = $invtype;
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
if (isset($_POST['Submit'])) {
if ($_POST['firstname'] != "") {
$_POST['firstname'] = filter_var($_POST['firstname'], FILTER_SANITIZE_STRING);
if ($_POST['firstname'] == "") {
$errors .= 'Please enter a valid first name.<br/><br/>';
}
} else {
$errors .= 'Please enter your first name.<br/>';
}
if ($_POST['lastname'] != "") {
$_POST['lastname'] = filter_var($_POST['lastname'], FILTER_SANITIZE_STRING);
if ($_POST['lastname'] == "") {
$errors .= 'Please enter a valid last name.<br/><br/>';
}
} else {
$errors .= 'Please enter your last name.<br/>';
}
if (!$errors) {header("location: offerform_switch.php");
}
else {
echo '<div style="color: red">' . $errors . '<br/>
</div>';
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Offer Form, Part 1</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" href="inventron_sage_short.css" type="text/css" />
<link rel="stylesheet" href="form.css" type="text/css" />
</head>
<body>
<div id = "logo">
<img src = "img/top.jpg" alt = "logo" />
</div>
<div id = "wrapper">
<div id="stylized" class="myform">
<form id="form" action="page1.php" method="post">
<p>
<label for="firstname">FIRST NAME*:
</label>
<input type="text" name="firstname" id="firstname" value="<?php echo $firstname?>" />
</p>
<p>
<label for="lastname">LAST NAME*:
</label>
<input type="text" name="lastname" id="lastname" value="<?php echo $lastname?>" />
</p>
<div id = "category">Categorize your invention:</div>
<div class="spacer"></div>
<p>
<select id="invtype" name="invtype">
<option value="0" selected="selected">Select type</option>
<option value="product">PRODUCT</option>
<option value="software">SOFTWARE</option>
</select>
<input type="submit" name="Submit" value="Next!" />
</div>
</div>
</body>
</html>
Here is my offerform_switch.php:
<?php
session_start();
// echo variable from the session, we set this on our other page
echo $_SESSION['invtype'];
$invtype = $_SESSION['invtype'];
//connect to your database ** EDIT REQUIRED HERE **
mysql_connect("mysql.myserver.com","myuser","mypassword"); //(host, username, password)
//specify database ** EDIT REQUIRED HERE **
mysql_select_db("invention") or die("Unable to select database"); //select which database we're using
switch ($invtype){
case "product":
include("page2_product.php");
break;
case "software":
include("page2_software.php");
break;
default:
echo "The invention type did not go through correctly.";
}
?>
What am I doing wrong?
Thank you!
It should be
$_SESSION['invtype'] = $_POST['invtype'];
You're missing "session_id();" right below the "session_start();". I don't know why exactly it's required, but if I remember correctly, it is.