会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132358个问题
WEB前端全系列/第五阶段:前后端交互/PHP、数据库编程与设计 136楼

老师我在添加文章时页面提示添加失败报错主键id不能为0,但查看数据库时发现添加了一条id=0的数据,是什么原因

news.php

<?php
    try{
        $con=new PDO("mysql:host=localhost;dbname=newsdb;charset=utf8","root","");
        $con->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
        $res=$con->query("select * from news");
        $data=$res->fetchAll(PDO::FETCH_ASSOC);
//      字符串拼接
 $str='';
        for($i=0;$i<count($data);$i++){
            $str.="<tr><td>{$data[$i]['id']}</td><td>{$data[$i]['title']}</td><td>{$data[$i]['author']}</td><td>{$data[$i]['des']}</td><td>{$data[$i]['content']}</td><td>{$data[$i]['time']}</td><td><a href='#'>删除</a><a href='#'>修改</a></td></tr>";
        }

    }catch(PDOException $e){
        echo $e->getMessage();
    }
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
 content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <div class="container">
        <div class="left">
            <a href="javascript:;">所有文档列表</a>
            <a href="addnews.html">添加文档</a>
        </div>
        <div class="right">
            <table border="1" cellpadding="0" cellspacing="0" width="100%">
                <tr>
                    <th>ID</th>
                    <th>标题</th>
                    <th>作者</th>
                    <th>描述</th>
                    <th width="40%">内容</th>
                    <th>时间</th>
                    <th>操作</th>
                </tr>
                <?php
                    echo $str;
                ?>
            </table>
        </div>
    </div>
</body>

addnews.html

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
 content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .container {width: 500px;margin: 100px auto;overflow: hidden;}
        .left,.right {float: left;}
        .left {width: 150px;}
        .right {width: 350px;}
        textarea {vertical-align: top;}
        button {display: block;width: 100px;height: 40px;margin: 10px auto;cursor: pointer;}
    </style>
</head>
<body>
    <div class="container">
        <div class="left">
            <a href="#">添加文档页面</a>
        </div>
        <div class="right">
            <form action="addnews.php" method="get">
                标题:<input type="text" name="title"><br/>
                作者:<input type="text" name="author"><br/>
                描述:<input type="text" name="des"><br/>
                内容:<textarea name="content" id="" cols="30" rows="10"></textarea>
                <button>提交</button>
            </form>
        </div>
    </div>
</body>
</html>

addnews.php

<?php
//    接收用户传来的数据
//    isset()判断传值是否为空
    $title=isset($_GET['title'])?$_GET['title']:'';
    $author=isset($_GET['author'])?$_GET['author']:'';
    $des=isset($_GET['des'])?$_GET['des']:'';
    $content=isset($_GET['content'])?$_GET['content']:'';
//    生成一个时间
    $time = time();
//    写入数据库
    $con=new PDO("mysql:host=localhost;dbname=newsdb;charset=utf8","root","");
    $con->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    $res=$con->query("insert into news (title,author,des,content,time) values ('{$title}','{$author}','{$des}','{$content}','{$time}')");
    if($res){
        echo "<script>alert('添加成功');window.location href='news.php'</script>";
    }else {
        print_r($con);
        echo "添加失败";
    }
?>

数据库设计

image.png

运行结果

image.png

image.png

WEB前端全系列/第五阶段:前后端交互/PHP、数据库编程与设计 137楼
WEB前端全系列/第五阶段:前后端交互/PHP、数据库编程与设计 138楼
WEB前端全系列/第五阶段:前后端交互/PHP、数据库编程与设计 139楼
WEB前端全系列/第五阶段:前后端交互/PHP、数据库编程与设计 142楼
WEB前端全系列/第五阶段:前后端交互/PHP、数据库编程与设计 143楼

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="js/jquery-1.12.3.min.js"></script>
    <style>
        *{
            margin: 0;
            padding: 0;
            user-select: none;
        }
        .box{
            width: 400px;
            height: 300px;
            margin: 100px auto;
            border: 1px solid;
            border-radius: 15%;
        }


        .box .innerbox{
            width: 240px;
            margin-left:100px ;
            position: relative;
            top: 80px;
        }


        .box .textOne{
            display: inline-block;
            width: 70px;
            height: 26px;
            text-align: right;
        }

        .box input{
            display: inline-block;
            width: 100px;
            height: 20px;
            margin-left: 2px;
            cursor: pointer;
        }


        .box .yanzhengma{
            display: inline-block;
            margin-bottom: 2px;
        }


        .box button{
            text-align: center;
            position: relative;
            margin-top: 10px;
            margin-left: 40px;
            cursor: pointer;
        }
    </style>
</head>
<body>
<div class="box">
    <div class="innerbox">
        <span class="textOne">账号:</span><input type="text"><br>
        <span class="textOne">密码:</span><input type="text"><br>
        <span class="textOne">验证码:</span><input type="text"><div class="yanzhengma"></div><br>
        <button>登录</button>
        <button>注册</button>
    </div>
</div>
<script>
    var yanzhengmaText=document.querySelectorAll('input')[2];
    var $buts=$('button');
    //验证码判断
    //创建验证码
    function getRandomNumber(min,max) {
        return Math.floor(Math.random()*(max-min)+min);
    }
    function yanzhengma(length){
        var yanzhengmaInner='';
        var characterDic =
            'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
        for (i=0;i<length;i++){
            var index=getRandomNumber(0,62);
            var zifu=characterDic[index];
            yanzhengmaInner+=zifu;
        }
        return yanzhengmaInner;
        //必须用return来使生成的验证码返回到调用者
    }
    var yanZ= yanzhengma(4);
    console.log(yanZ);
    var yanzheng=document.querySelector('.yanzhengma');
    yanzheng.innerHTML=yanZ;
    yanzheng.style.cssText='background-color:gray;width:60px;line-height:22px; text-align: center;margin-left:1px;vertical-align: bottom;';
    //验证码判断
    var yanzhengmaSpan=document.querySelector('.yanzhengmatiishi');
    // console.log(yanzhengmaText);
    // console.log(yanzheng);


    /*yanzhengmaText.onblur=function () {
        var yanzhengmaTextneirong=yanzhengmaText.value;
        if (yanzhengmaTextneirong==yanZ){
            yanzhengmaSpan.innerHTML='验证码正确';
            yanzhengmaSpan.style.cssText='color:green;font-size:10px;';
            yanzhengmaText.setAttribute('class','borderGreen');
        }
        else {
            yanzhengmaSpan.innerHTML='验证码不正确,请区分大小写';
            yanzhengmaSpan.style.cssText='color:red;font-size:10px;';
            yanzhengmaText.setAttribute('class','borderRed');
        }
    }*/
    //验证码判断
    //创建验证码

//点击登录验证验证码
    $buts.eq(0).click(function () {
        /*var yanzhengmaTextneirong=yanzhengmaText.value;
        if (yanzhengmaTextneirong==yanZ){
            alert('验证码正确');
/!*            yanzhengmaSpan.innerHTML='验证码正确';
            yanzhengmaSpan.style.cssText='color:green;font-size:10px;';
            yanzhengmaText.setAttribute('class','borderGreen');*!/
        }
        else {
            alert('验证码不正确,请区分大小写');
            /!*yanzhengmaSpan.innerHTML='验证码不正确,请区分大小写';
            yanzhengmaSpan.style.cssText='color:red;font-size:10px;';
            yanzhengmaText.setAttribute('class','borderRed');*!/
        }*/



        // 传递前端数据到php文件
        $.ajax({
            type:'post',
            url:'4.1phplogin.php',
            data:'json',
            data:{
                loginName:$('input').eq(0).val(),
                loginPassword:$('input').eq(1).val()
            },
            success:function (res) {
                // console.log(res);

                if(res.return1==0){
                    alert('登录成功!');
                }else  if(res.return1==1){
                    alert('登陆失败,账号或密码错误');
                }else{
                    alert('网络连接失败');
                }
            }
        })
    })


</script>
</body>
</html>

老师,这个是php文件

<?php
/*$username=$_POST['loginName'];
$paswword=$_POST['loginPassword'];
$success=array('msg'=>'ok');*/root
$con=mysqli_connect('localhost','root','','beixidb');
//var_dump($con);
if($con){
     mysqli_query($con,'set names uft8');
     mysqli_query($con,'set character_set_client uft8');
     mysqli_query($con,'set character_set_results uft8');

     $sql="select * from stu where 1";
     $result=$con->query($sql);
//     echo($result->num_rows);//有行数显示
     if($result->num_rows>0){
     $info=[];
        for($i=0;$row=$result->fetch_assoc();$i++){
           $info[$i]=$row;
          }
        $flag=0;
         for($j=0;$j<count($info);$j++){
          if($info[$j]['name']=='$username'){
             if($info[$j]['password']=='$paswword'){
                 $success['return1']=0;
                 $flag=1;
                 break;
             }

          }


         }

     if($flag==0){
        $success['return1']=1;

        }
        else{
           $success['return1']=1;
         }


     }
   }else{
    $success['return1']=2;
   }

//返回给前台
    echo json_encode($success);

?>


麻烦您了,老师

WEB前端全系列/第五阶段:前后端交互/PHP、数据库编程与设计 147楼
WEB前端全系列/第五阶段:前后端交互/PHP、数据库编程与设计 150楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637