关于XXX.php?id=,XXX.php中的变量id可用的条件是什么?
初学php,遇到很多问题,这个问题实在暂时没想明白,希望大家帮看看。
<br /><?php <br /> include("conn.php"); <br /> if(!empty($_GET['id']))<br /> {<br /> $id=$_GET['id'];<br /> $sql="select * from `user` where `id`='$id'";<br /> $query=mysql_query($sql);<br /> $result=mysql_fetch_array($query);<br /> } <br /> if(!empty($_POST['sub']))<br /> {<br /> //$id=$_GET['id'];<br /> $title=$_POST['title'];<br /> $content=$_POST['content'];<br /> $id=$_POST['hid'];<br /> $sql="update `user` set `title` = '$title', `content` = '$content' where id='$id limit 1'";<br /> mysql_query($sql);<br /> echo "<script> alert('更新完成'); location.href='index.php'</script>";<br /> }<br />?><br /><form action="edit.php" method="post"><br /><input type="hidden" name="hid" value="<?php echo $result['id'] ?>"><br><br />标题<input type="text" name="title" value="<?php echo $result['title'] ?>"><br><br />内容<textarea rows="5" cols="50" name="content"><?php echo $result['content'] ?></textarea><br><br /><input type="submit" name="sub" value="提交"><br /></form><br /><br />
刚看了php100的视频,讲解关于微型博客。其中实现博客的编辑功能,在index.php中放入一个编辑的链接,如下:
<br /><h2 id="php-nbsp-echo-nbsp-result-title-nbsp-br-a-nbsp-href-nbsp-nbsp-edit-php-id-php-nbsp-echo-nbsp-result-id-编辑-a-nbsp-br-a-nbsp-href-nbsp-nbsp-delete-php-id-php-nbsp-echo-nbsp-result-id-删除-a"><?php echo $result['title']; ?><br /> |<a href = "edit.php?id=<?php echo $result['id'];?>">编辑</a> <br /> <a href = "delete.php?id=<?php echo $result['id'];?>">删除</a>|</h2><br />
问题是,为什么当我在edit.php中的第二个if里不能直接用这个id调用(如代码中被注释的一行,如果这样,会报Undefined index id而且编辑不生效),而是要在表单里再重新插入hidden?
------解决方案--------------------
">编辑
这个应该是列表页面进入编辑页面的url,使用 是用$_GET 获取的,代码没错。
然后进入到编辑页面,修改内容后,你按submit提交。表单的method是$_POST。所以$_GET['id']当然没有数据,因为要用$_POST获取。但你加了hidden,并用$id=$_POST['hid'];获取到了。
<br /> if(!empty($_POST['sub']))<br /> {<br /> //$id=$_GET['id'];<br /> $title=$_POST['title'];<br /> $content=$_POST['content'];<br /> $id=$_POST['hid'];<br /> $sql="update `user` set `title` = '$title', `content` = '$content' where id='$id limit 1'";<br /> mysql_query($sql);<br /> echo "<script> alert('更新完成'); location.href='index.php'</script>";<br /> }<br />
代码可以这样优化:
<br />include("conn.php"); <br /><br />if(isset($_POST['sub'])){ // 判斷是否提交表單<br /> $title=$_POST['title'];<br /> $content=$_POST['content'];<br /> $id=$_POST['hid'];<br /> $sql="update `user` set `title` = '$title', `content` = '$content' where id='$id limit 1'";<br /> mysql_query($sql);<br /> echo "<script> alert('更新完成'); location.href='index.php'</script>";<br /> exit();<br />}else{ // 不是提交表單,表示是從列表頁過來<br /> $id=$_GET['id'];<br /> $sql="select * from `user` where `id`='$id'";<br /> $query=mysql_query($sql);<br /> $result=mysql_fetch_array($query);<br />}<br />?><br /><br /><form action="edit.php" method="post"><br /><input type="hidden" name="hid" value="<?php echo $result['id'] ?>"><br><br />标题<input type="text" name="title" value="<?php echo $result['title'] ?>"><br><br />内容<textarea rows="5" cols="50" name="content"><?php echo $result['content'] ?></textarea><br><br /><input type="submit" name="sub" value="提交"><br /></form><br />
------解决方案--------------------
edit.php 具有两个不同的功能,在修改微博的过程中将执行两次
第一次:edit.php?id=nnn
有 url 参数,程序走 if(!empty($_GET['id'])) 真 分支
完成查询数据
并充填表单
第二次 表单提交 edit.php
没有 url 参数,程序走 if(!empty($_POST['sub'])) 真 分支
完成数据修改后跳转至目录页
由于两个条件不会同时成立,所以 $id=$_GET['id'] 在第二个 if 中不可用
此程序有一个潜在的问题:
当直接浏览器访问 edit.php 时,因为两个条件都不成立。会在充填表单时发生错误,而暴露服务器布局
如果忽略错误检查,此时应为新微博输入。但 edit.php 并无新增微博的处理代码
而当做修改的话,又因缺少 id 造成修改失败。还会给出'更新完成'的虚假信息

TheSecretTokeEpingAphp-PowerEdwebSiterUnningSmoothlyShyunderHeavyLoadInVolvOLVOLVOLDEVERSALKEYSTRATICES:1)emplactopCodeCachingWithOpcachingWithOpCacheToreCescriptexecution Time,2)使用atabasequercachingCachingCachingWithRedataBasEndataBaseLeSendataBaseLoad,3)

你應該關心DependencyInjection(DI),因為它能讓你的代碼更清晰、更易維護。 1)DI通過解耦類,使其更模塊化,2)提高了測試的便捷性和代碼的靈活性,3)使用DI容器可以管理複雜的依賴關係,但要注意性能影響和循環依賴問題,4)最佳實踐是依賴於抽象接口,實現鬆散耦合。

是的,優化papplicationispossibleandessential.1)empartcachingingcachingusedapcutorediucedsatabaseload.2)優化的atabaseswithexing,高效Quereteries,and ConconnectionPooling.3)EnhanceCodeWithBuilt-unctions,避免使用,避免使用ingglobalalairaiables,並避免使用

theKeyStrategiestosigantificallyBoostPhpaPplicationPerformenCeare:1)UseOpCodeCachingLikeLikeLikeLikeLikeCacheToreDuceExecutiontime,2)優化AtabaseInteractionswithPreparedStateTementStatementStatementAndProperIndexing,3)配置

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增強codemodocultion,可驗證性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

選擇DependencyInjection(DI)用於大型應用,ServiceLocator適合小型項目或原型。 1)DI通過構造函數注入依賴,提高代碼的測試性和模塊化。 2)ServiceLocator通過中心註冊獲取服務,方便但可能導致代碼耦合度增加。

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)啟用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替換loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

phpemailvalidation invoLvesthreesteps:1)格式化進行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

記事本++7.3.1
好用且免費的程式碼編輯器