插入查询不适用于MySQL,PHP

时间:2016-02-13 14:57:57

标签: php mysql mysqli

这是我在MySQL中作为后端和PHP作为前端插入数据的代码 正确建立连接但是插入查询不起作用,它也不会根据插入查询后的结尾处的其他条件显示任何错误 它也没有到达$ _POST ['submit']内部。

<html>
<head></head>
<title></title>
<body>
    <form type="post" name="addimage" enctype="multipart/form-data" >
     Album Name<input type="text" name="albumname">
    <input type="file" name="filesToUpload" id="filesToUpload" multiple=""/>
    </p>
       Client Name<input type="text" name="clientname">
       <br>Location<input type="text" name="location">
        <button type="submit" value="submit" name="submit" id="submit">Submit</button>
</body>
</form>
</html>
<?php
 $servername = "localhost";
 $username = "root";
 $password = "";
 $dbname = "apostrophe";
 $con=mysqli_connect("localhost","root","","apostrophe");
 mysqli_select_db($con,"apostrophe");
if(isset($_POST['submit']))
{
echo "reached";
$albumname=$_REQUEST['albumname'];
$images=$_REQUEST['filesToUpload'];
$client=$_REQUEST['clientname'];
$loc=$_REQUEST['loc'];
echo "reached submit";
$sql="INSERT INTO album(albumname,images,clientname,location)VALUES('$albumname','$albumname','$client','$loc')";
echo "reached down";
if($con->query($sql)===TRUE)
{
 echo "Success";
}
else
echo "Failed";
}
?>

2 个答案:

答案 0 :(得分:2)

原始代码中存在相当多的错误(形式跨越body,表单方法的错误声明,头部之外的标题等)并且没有尝试处理实际上传的图像。希望下面的内容应该让你完成文件处理工作的开头 - 虽然毫无疑问我也错过了一些东西' - )

<?php
    $status='';
    /* Might as well test that all necessary fields are included in form submission */
    if( isset( $_POST['submit'], $_POST['albumname'], $_POST['clientname'], $_POST['location'] ) ){

        /* only need to declare the db connection if the variables are set */
        $servername = "localhost";
        $username = "root";
        $password = "";
        $dbname = "apostrophe";

        /* create the db conn */
        $con=mysqli_connect("localhost","root","","apostrophe");

        /* at least some basic filtering if you intend to place user submitted content directly in the sql */
        $albumname=strip_tags( filter_input( INPUT_POST,'albumname',FILTER_SANITIZE_STRING ) );
        $client=strip_tags( filter_input( INPUT_POST,'clientname',FILTER_SANITIZE_STRING ) ); 
        $loc=strip_tags( filter_input( INPUT_POST,'location',FILTER_SANITIZE_STRING ) );



        /* handle file uploads */
        $fieldname='filesToUpload';

        foreach( $_FILES[$fieldname]['name'] as $i => $name ) {
            if( !empty( $_FILES[$fieldname]['tmp_name'][$i] ) ) {

                $filename = $_FILES[$fieldname]['name'][$i];
                $size = $_FILES[$fieldname]['size'][$i];
                $type = $_FILES[$fieldname]['type'][$i];
                $tmpfile  = $_FILES[$fieldname]['tmp_name'][$i];

                /* copy file to final destination - this is not complete!! */
                $bytes=move_uploaded_file( $tmpfile, '/path/to/final/directory/'.$filename );

                /* to debug uncomment below */
                #echo $filename,$tmpfile,$size,$type;
            }
        }

        /* prepare and execute sql */
        $sql="INSERT INTO `album` ( `albumname`, `images`, `clientname`, `location` ) VALUES ( '$albumname', '$albumname', '$client', '$loc' )";

        /* set status variable to be displayed under form */
        $status=( $con->query( $sql )===TRUE ) ?  "Success" : "Failed";

    } else {
        $status='bad foo';
        $status=print_r( $_POST, true );
    }
?>
<html>
    <head>
        <title>File upload and database inserts</title>
    </head>
    <body>
        <form method="post" name="addimage" enctype="multipart/form-data" >
            Album Name<input type="text" name="albumname">
            <input type="file" name="filesToUpload[]" id="filesToUpload" multiple=""/>

            Client Name<input type="text" name="clientname">
            <br>
            Location<input type="text" name="location">
            <button type="submit" value="submit" name="submit" id="submit">Submit</button>
        </form><?php echo $status; ?>
    </body>
</html>

答案 1 :(得分:0)

更改您的行:
     mydata <- structure(list(A0AUT = c("no_change", "no_change", "no_change", "no_change", "no_change", "no_change"), A0AYT = c("no_change", "no_change", "no_change", "up", "up", "up"), A0AZT = c("no_change", "down", "no_change", "no_change", "no_change", "no_change"), A0B2T = c("no_change", "no_change", "no_change", "no_change", "no_change", "no_change"), A0B3T = c("no_change", "no_change", "no_change", "up", "no_change", "no_change")), .Names = c("A0AUT", "A0AYT", "A0AZT", "A0B2T", "A0B3T"), class = "data.frame", row.names = c("100130426", "100133144", "100134869", "10357", "10431", "136542"))

     <form type="post" name="addimage" enctype="multipart/form-data">