简体   繁体   中英

When I click submit on my php form the landing page comes up blank and it does not submit date to the database

As mentioned in the title when I click submit on my php form the landing page comes up blank and it does not submit date to the database. It worked before (I must have done something incorrect) and actually occassionaly (extremely rarely) works, although I am not sure why. Below is the landing page for the form. And below that is the form itself. I am quite new to this so any help/comments are appreciated.

Form Landing Page

 <?php
    session_start();
    if(isset($_SESSION['myusername']))
    if ($_POST['title'] == '' or $_POST['story'] == '' or $_POST['city'] == '' or $_POST['province'] == '' ) {
    $_SESSION['error1'] = "All fields required!";
    header("Location: debate.php");
    print "issue";
    }
    else {
    include 'config.php';
    if (get_magic_quotes_gpc())
    {
    function stripslashes_deep($value)
    {
    $value = is_array($value)?
    array_map('stripslashes_deep',$value):
    stripslashes($value);
    return $value;
    }

    $_POST = array_map('stripslashes_deep',$_POST);
    $_GET = array_map('stripslashes_deep',$_GET);
    $_COOKIE = array_map('stripslashes_deep',$_COOKIE);
    $_REQUEST = array_map('stripslashes_deep',$_REQUEST);
    }
    $name = mysql_real_escape_string($_POST['title']);
    $content = mysql_real_escape_string($_POST['story']);
    $city = mysql_real_escape_string($_POST['city']);
    $province = mysql_real_escape_string($_POST['province']);
    $query = "INSERT INTO comments(commentusername, commentcontent, status, debateid, city, province) VALUES ('.$name.','.$content.','0','1','.$city.','.$province.')";
    $result=mysql_query($query,$conn);
    $_SESSION['error1'] = "Thank you for your submission!";
    header("Location: debate.php");
    print "issue";
    mysql_close();
    }?>

Submission Form

Submission form coding.

<!-- comment form -->
<div style="position:absolute;left:600px;top:750px;border-style:solid;border-color:#970033;background-color:black;border-width:4px;">
<div align="center"><strong><font color="white"><b>Join the debate</b></font></strong></div>
<form action="cposting.php" method="post">
    <TABLE WIDTH="60%">
     <tr>
      <td>Name:<input type="text" name="title" SIZE="35"> </td>
 <tr>
      <td>City:<br><input type="text" name="city" SIZE="35"> </td>
     </tr>
<tr>
      <td>Province:<input type="text" name="province" SIZE="35"> </td>
     </tr>
     <tr><td>Comment<br></td></tr>
       <tr width="80%">

      <td><TEXTAREA NAME="story" ROWS="5" COLS="30"></TEXTAREA></td>
     </tr>
 <td><input type="submit" value="Post"></td></tr>
</table>
</form>

I really apprecate your help. Thanks!

The code below is for the admin approval page. This is where the comments go when I post for the first time (and subsequent posts if I leave my browser open) but do not go after this.

<?php
session_start();
if(isset($_SESSION['myusername'])) 
if($_SESSION['myusername'] == "Bobert") {
?>
<html>
<title>Discourse Magazine</title>
<head>
</head>
<body background="" style="background: url() no-repeat;  background-size: 100%;">

<?php
include 'config.php';

print '<h4>'.'Welcome back:'.'&nbsp'.$_SESSION['myusername'].'</h4>';
?>
<div style="float:left;border-style:solid;border-width:2px;"><img style="z-index:1;height:200px;" src="/favicon.png" alt="" /></div><br><br><br><br><br><br><br><br><br><br><br><br><br>
Approving Comments (listed oldest to newest)
<?php
if (get_magic_quotes_gpc()) {
    $process = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST);
    while (list($key, $val) = each($process)) {
        foreach ($val as $k => $v) {
            unset($process[$key][$k]);
            if (is_array($v)) {
                $process[$key][stripslashes($k)] = $v;
                $process[] = &$process[$key][stripslashes($k)];
            } else {
                $process[$key][stripslashes($k)] = stripslashes($v);
            }
        }
    }
    unset($process);
}


if ($conn) {

$SQL = "SELECT * FROM comments where status = '0' ORDER BY commentid";
$result = mysql_query($SQL,$conn) OR die(mysql_error());
while ($row = mysql_fetch_array($result)) {


$rows=mysql_num_rows($result);
$capproveidid = $row['commentid'];
$cdeleteid = $row['commentid'];
print '&nbsp';
print '<div style="background: url() no-repeat;overflow:auto;padding-top:3px;border-style:solid;border-width:5px;width:800px;">';
print '<p style="padding-left:5px;">'.'<u>'.'<strong>'. $row['commentusername'].'</strong>'.'</u>' .'<br>'. $row['commentcontent'] .'<br>'.'<br>'.'</p>';
?>

<form action="approve.php" method="post">
<input type="hidden" name="capproveidid" value="<?php echo $capproveidid;?>"/>
<input type="submit" value="Approve">
</form>
<form action="cdelete.php" method="post">
<input type="hidden" name="cdeleteid" value="<?php echo $cdeleteid;?>"/>
<input type="submit" value="Delete">
</form>

<?php
print '</div>';

}
}
else {
print "Database NOT Found ";
}
}
else{
header('Location: debate.php');
}
ob_end_flush();
?>
</body>
</html>

You're most likely not properly formatting your INSERT query. By using double quotes around the entire query, you're literally sending ...'.something.'... .

You can either do this:

$query = "INSERT INTO comments(commentusername, commentcontent, status, debateid, city, province) VALUES ('$name','$content','0','1','$city','$province')";

Or this:

$query = "INSERT INTO comments(commentusername, commentcontent, status, debateid, city, province) VALUES ('" . $name . "','" . $content . "','0','1','" . $city . "','" . $province . "')";

Either way, you can debug by adding ... or die(mysql_error()); after your mysql_query . Additionally, you can just echo $query; to see what is being sent.

Lastly, you should stop using mysql_ functions as they are being deprecated and use mysqli_ or PDO which allow for prepared statements and prevent against SQL injection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM