$result
別のページから別のページへの値の受け渡しに問題があります。
このページPOST
に値を入力したとき。$result
それは正常に動作し、エコーアウトします。
$result
しかし、これをフォームに渡しisset()
て自分自身に投稿しようとすると。
値は未定義インデックスになります。
ページ1
<?PHP
$connect=mysql_connect('localhost', 'root', '');
$db=mysql_select_db('survey', $connect);
$SQL="SELECT * FROM question";
$data=mysql_query($SQL);
$num=mysql_num_rows($data);
$start=0;
?>
<style type="text/css">
#survey{
width:100%; height:50px;
background:#e6e6e6; border-bottom:solid 1px white;
}
#survey form{
position:relative;
left:320px; top:15px;
float:left;
}
#survey form a{
text-decoration:none;
color:black;
}
.submit{
background-color: transparent;
border: none;
cursor: pointer;
}
</style>
<html>
<div id="container">
<?PHP
if($num<3){
echo "
<div id='create'><a href='question.php'>+ Create a Survey</a></div>
";
}
?>
<?PHP
while($start<$num){
$result=mysql_result($data, $start, 'ID');
$list_num=$start+1;
$ID=$result['ID'];
$list="
<div id='survey'>
<span style='font-size:24px; color:gray; position:relative; float:left; left:10px; top:13px;'>
SURVEY 0$list_num
</span>
<form action='survey.php' method='POST'>
<input class='submit' type='submit' name='submit1' value='View' />
<input type='hidden' name='result' value='$ID'/>
</form>
<form action='update.php' method='POST'>
<input class='submit' type='submit' name='submit2' value='Edit' />
<input type='hidden' name='result' value='$ID'/>
</form>
<form action='delete.php' method='POST'>
<input class='submit' type='submit' name='submit3' value='✕ Delete' />
<input type='hidden' name='result' value='$ID'/>
</form>
</div>
";
echo $list;
$start++;
}
?>
</div>
</html>
ページ2
<?PHP
$result=$_POST['result'];
$error="";
if(isset($_POST['submit'])){
$question=$_POST['question'];
$answer_a=$_POST['a'];
$answer_b=$_POST['b'];
$answer_c=$_POST['c'];
if($question && $answer_a && $answer_b && $answer_c){
$connect=mysql_connect("localhost", "root", "");
$db=mysql_select_db("survey", $connect) or die("");
$SQL="UPDATE question SET question='$question', answer_a='$answer_a', answer_b='$answer_b', answer_c='$answer_c'
WHERE ID='$result'";
mysql_query($SQL);
$SQL="UPDATE answer SET question='$question', answer_a='0', answer_b='0', answer_c='0'
WHERE ID='$result' ";
mysql_query($SQL);
header("location:create.php");
}
else{$error="<div style='color:white; background:gray; width:200px; height:20px; text-align:center; line-height:20px;'>"
. "Please fill all the field." . "</div>";}
}
?>
<style>
body{
font-family:Arial, Helvetica, sans-serif;
font-size:14px;
}
</style>
<html>
<form action="update.php" method="POST">
Question<br /><input type="text" name="question" placeholder=" Enter your question here" maxlength="50" /><br/><br/>Answer<br/>
a<input type="text" name="a" placeholder=" Answer a" maxlength="50"/>
b<input type="text" name="b" placeholder=" Answer b" maxlength="50"/>
c<input type="text" name="c" placeholder=" Answer c" maxlength="50"/>
<input type="submit" name="submit" value="SAVE"/>
</form>
<?PHP echo $error; ?>
</html>