HTML表单和PHP查询不向MySQL数据库提交数据

时间:2011-04-02 02:09:50

标签: php mysql

下面是一个应该将信息提交到MySQL数据库的表单。

下面的提交表单及其相应的查询文件无效。我填写所有字段,然后单击“提交”,我没有收到任何错误消息。

在submit2a.php上,我有mysql_connect() or die(mysql_error());mysql_select_db() or die(mysql_error());并且我没有收到错误,因此我认为它正在连接到MySQL。

但是当我检查MySQL数据库时,我添加的新信息不存在。

有没有人在下面的代码中看到任何明显的错误?

提前致谢,

约翰

表格:

echo '<form enctype="multipart/form-data" action="http://www...com/.../submit2a.php" method="post"> 
    <input type="hidden" value="'.$_SESSION['loginid'].'" name="uid">  

    <div class="submissiontitle"><label for="title">Blog Post Title:</label></div> 
    <div class="submissionfield"><input class="checkMax3" name="title" type="title" id="title" maxlength="80"></div>  


    <div class="texttitle"><label for="text1">Blog Post Text 1:</label></div> 
    <div class="textfield"><textarea class="checkMax" name="text1" type="comment" id="text1" maxlength="10000"></textarea></div>

    <div class="imagetitle"><label for="image1">Image 1:</label></div> 
    <div class="imagefield"><input type="file" name="image1" /></div>   

    <div class="hyperlinktitle"><label for="url1">Hyperlink 1:</label></div> 
    <div class="hyperlinkfield "><input name="url1" type="title" id="url1" maxlength="200"></div>



    <div class="texttitle2"><label for="text2">Blog Post Text 2:</label></div> 
    <div class="textfield2"><textarea class="checkMax" name="text2" type="comment" id="text2" maxlength="10000"></textarea></div>

    <div class="imagetitle2"><label for="image2">Image 2:</label></div> 
    <div class="imagefield2"><input type="file" name="image2" /></div>  

    <div class="hyperlinktitle2"><label for="url2">Hyperlink 2:</label></div> 
    <div class="hyperlinkfield2"><input name="url2" type="text" id="url2" maxlength="200"></div>



    <div class="texttitle3"><label for="text3">Blog Post Text 3:</label></div> 
    <div class="textfield3"><textarea class="checkMax" name="text3" type="comment" id="text3" maxlength="10000"></textarea></div>

    <div class="imagetitle3"><label for="image3">Image 3:</label></div> 
    <div class="imagefield3"><input type="file" name="image3" /></div>  

    <div class="hyperlinktitle3"><label for="url3">Hyperlink 3:</label></div> 
    <div class="hyperlinkfield3"><input name="url3" type="text" id="url3" maxlength="200"></div>



    <div class="texttitle4"><label for="text4">Blog Post Text 4:</label></div> 
    <div class="textfield4"><textarea class="checkMax" name="text4" type="comment" id="text4" maxlength="10000"></textarea></div>

    <div class="imagetitle4"><label for="image4">Image 4:</label></div> 
    <div class="imagefield4"><input type="file" name="image4" /></div>  

    <div class="hyperlinktitle4"><label for="url4">Hyperlink 4:</label></div> 
    <div class="hyperlinkfield4"><input name="url4" type="text" id="url4" maxlength="200"></div>





    <div class="submissionbutton"><input name="submit" type="submit" value="Submit"></div> 
</form>
';

对submit2a.php的查询:

$remove_array = array('http://www.', 'http://', 'https://', 'https://www.', 'www.');

$uid = $_POST['uid'];

$title = $_POST['title'];

$text1 = $_POST['text1'];

$image1 = $_POST['image1'];

$image1 = $_FILES['image1'];

$url1 = $_POST['url1'];



$text2 = $_POST['text2'];

$image2 = $_POST['image2'];

$image2 = $_FILES['image2'];

$url2 = $_POST['url2'];



$text3 = $_POST['text3'];

$image3 = $_POST['image3'];

$image3 = $_FILES['image3'];

$url3 = $_POST['url3'];



$text4 = $_POST['text4'];

$image4 = $_POST['image4'];

$image4 = $_FILES['image4'];

$url4 = $_POST['url4'];







$cleanurl1 = str_replace($remove_array, "", $_POST['url1']);
$cleanurl1 = strtolower($cleanurl1);
$cleanurl1 = preg_replace('/\/$/','',$cleanurl1);
$cleanurl1 = stripslashes($cleanurl1);
$cleanurl1 = mysql_real_escape_string(trim($cleanurl1));

$cleanurl2 = str_replace($remove_array, "", $_POST['url2']);
$cleanurl2 = strtolower($cleanurl2);
$cleanurl2 = preg_replace('/\/$/','',$cleanurl2);
$cleanurl2 = stripslashes($cleanurl2);
$cleanurl2 = mysql_real_escape_string(trim($cleanurl2));


$cleanurl3 = str_replace($remove_array, "", $_POST['url3']);
$cleanurl3 = strtolower($cleanurl3);
$cleanurl3 = preg_replace('/\/$/','',$cleanurl3);
$cleanurl3 = stripslashes($cleanurl3);
$cleanurl3 = mysql_real_escape_string(trim($cleanurl3));

$cleanurl4 = str_replace($remove_array, "", $_POST['url4']);
$cleanurl4 = strtolower($cleanurl4);
$cleanurl4 = preg_replace('/\/$/','',$cleanurl4);
$cleanurl4 = stripslashes($cleanurl4);
$cleanurl4 = mysql_real_escape_string(trim($cleanurl4));



//$url = $_POST['url'];

//$subcheck = (isset($_POST['subcheck'])) ? 1 : 0;
$title = mysql_real_escape_string($title);
$title = stripslashes($title);
$slug = str_replace(' ', '-', $title);




echo '-'.$site1.'-';



$site1 = 'http://' . $cleanurl1;
$site2 = 'http://' . $cleanurl2;
$site3 = 'http://' . $cleanurl3;
$site4 = 'http://' . $cleanurl4;

$displayurl = parse_url($site1, PHP_URL_HOST);




    $query = sprintf("INSERT INTO submission VALUES (NULL, '$title', '$text1', '$text2', '$text3', '$text4', '$site1', '$site2', '$site3', '$site4', NULL)")or die(mysql_error());


mysql_query($query, $db);

        $id = (int) mysql_insert_id($db);   

$submissionid = $id;        



$info1 = getImageSize($image1['tmp_name']);

$queryimage1 = sprintf(
            "insert into images (NULL, submissionid, filename, mime_type, file_size, file_data)
                values ('%s', '%s', '%s', %d, '%s')",
            $submissionid,
            mysql_real_escape_string($image1['name']),
            mysql_real_escape_string($info1['mime']),
            $image1['size'],
            mysql_real_escape_string(
                file_get_contents($image1['tmp_name'])
            )
        )or die(mysql_error());





$info2 = getImageSize($image2['tmp_name']);

$queryimage2 = sprintf(
            "insert into images (NULL, ubmissionid, filename, mime_type, file_size, file_data)
                values ('%s', '%s', '%s', %d, '%s')",
            $submissionid,
            mysql_real_escape_string($image2['name']),
            mysql_real_escape_string($info2['mime']),
            $image2['size'],
            mysql_real_escape_string(
                file_get_contents($image2['tmp_name'])
            )
        )or die(mysql_error());     




 $info3 = getImageSize($image3['tmp_name']);

$queryimage3 = sprintf(
            "insert into images (NULL, submissionid, filename, mime_type, file_size, file_data)
                values ('%s', '%s', '%s', %d, '%s')",
            $submissionid,
            mysql_real_escape_string($image3['name']),
            mysql_real_escape_string($info3['mime']),
            $image3['size'],
            mysql_real_escape_string(
                file_get_contents($image3['tmp_name'])
            )
        )or die(mysql_error());



 $info4 = getImageSize($image4['tmp_name']);

$queryimage4 = sprintf(
            "insert into images (NULL, submissionid, filename, mime_type, file_size, file_data)
                values ('%s', '%s', '%s', %d, '%s')",
            $submissionid,
            mysql_real_escape_string($image4['name']),
            mysql_real_escape_string($info4['mime']),
            $image4['size'],
            mysql_real_escape_string(
                file_get_contents($image4['tmp_name'])
            )
        )or die(mysql_error());     










header("Location: http://www...com/.../");
exit(); 

2 个答案:

答案 0 :(得分:1)

在第一个实例中,我觉得你在大多数语句中都没有调用“mysql_query”。即$ queryimage1,queryimage2,......

当前声明:

$queryimage1 = sprintf(
            "insert into images (NULL, submissionid, filename, mime_type, file_size, file_data)
                values ('%s', '%s', '%s', %d, '%s')",
            $submissionid,
            mysql_real_escape_string($image1['name']),
            mysql_real_escape_string($info1['mime']),
            $image1['size'],
            mysql_real_escape_string(
                file_get_contents($image1['tmp_name'])
            )
        )or die(mysql_error());

将是:

$queryimage1 = mysql_query(sprintf(
            "insert into images (NULL, submissionid, filename, mime_type, file_size, file_data)
                values ('%s', '%s', '%s', %d, '%s')",
            $submissionid,
            mysql_real_escape_string($image1['name']),
            mysql_real_escape_string($info1['mime']),
            $image1['size'],
            mysql_real_escape_string(
                file_get_contents($image1['tmp_name'])
            )
        ))or die(mysql_error());

答案 1 :(得分:0)

你应该使用MYSQLI或MYSQLI_PREPARE!