weixin_33709219 2016-04-24 12:33 采纳率: 0%
浏览 13

PHP错误:未定义索引

I'm newbie on Ajax and PHP. Decided to create index.php (using ajax) and simpan.php

<?php
include 'koneksi.php'; //conn
$target_dir="foto/";
$target_file=$target_dir.basename($_FILES['foto']['name']); //error
move_uploaded_file($_FILES['foto']['tmp_name'],$target_file); //error
$q=$db->prepare("insert into mahasiswa values
(?,?,?,?,?)");
$param=array($_POST['nim'],$_POST['nama'],
            $_POST['alamat'],$_POST['email'],
            $_FILES['foto']['name']); //error
$q->execute($param);
if($q){
    echo "OK";
}else{
    echo "fail";
}

I'm confused it has some errors:

Undefined index: foto in C:\xampp\htdocs\lat_ajax204\simpan.php on line 4

Undefined index: foto in C:\xampp\htdocs\lat_ajax204\simpan.php on line 5

Undefined index: foto in C:\xampp\htdocs\lat_ajax204\simpan.php on line 10

Fatal error: Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'foto' cannot be null in C:\xampp\htdocs\lat_ajax204\simpan.php:11 Stack trace: #0 C:\xampp\htdocs\lat_ajax204\simpan.php(11): PDOStatement->execute(Array) #1 {main} thrown in C:\xampp\htdocs\lat_ajax204\simpan.php on line 11

I create a mysql column in mahasiswa table for foto, the type is blob. Does it make the problem? But index.php has no error at all. Thank you

  • 写回答

1条回答 默认 最新

  • weixin_33701251 2016-04-24 12:44
    关注

    Use isset() to check if the form is submitted.

    http://php.net/manual/en/function.isset.php

    <?php
    include 'koneksi.php'; //conn
    if(isset($_FILES['foto'],$_POST['nim'],$_POST['nama'],$_POST['alamat'],$_POST['email'])) {
      $target_dir="foto/";
      $target_file=$target_dir.basename($_FILES['foto']['name']); //error
      move_uploaded_file($_FILES['foto']['tmp_name'],$target_file); //error
      $q=$db->prepare("insert into mahasiswa values
      (?,?,?,?,?)");
      $param=array($_POST['nim'],$_POST['nama'],
                  $_POST['alamat'],$_POST['email'],
                  $_FILES['foto']['name']); //error
      $q->execute($param);
      if($q){
          echo "OK";
      }else{
          echo "fail";
      }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 BDSBAS-B1C和B1C信号有什么不同
  • ¥15 在半圆平面内随机生成点坐标
  • ¥15 系统容量变化的几种多址方式TDMA, CDMA,FDMA,OFDMA 对比,应该给的是一个曲线 图,随着系统容量的增加,几种多址方式性能的对比 图,MATLAB程序仿真折线图
  • ¥15 用visual Studio 写c ++只运行上一个旧代码的运行结果是怎么回事
  • ¥15 系统容量变化的几种多址方式(TDMA,FDMA,OFDMA,CDMA)对比(相关搜索:曲线图)
  • ¥15 worldclim 历史及未来气候数据矫正
  • ¥15 ajax服务器不能下载
  • ¥15 运用c++和opencv实现二维码的识别和三维坐标的建立
  • ¥100 理想汽车的ADB为什么到了国外换了SIM就可以打开?
  • ¥15 k210烧入flash报错error:2005