【问题标题】:RadioButton Value doesn't insert into mySql DB whit Php单选按钮值不会插入到 mySql DB whit Php
【发布时间】:2018-11-09 10:43:53
【问题描述】:

在通过 PHP 和 MySQL 进行简单的插入查询时,我发现了以下问题: 它没有插入radioButton的所有值..实际上实际上只插入了一个:( 我不明白为什么! 我认为 $ _POST 存在问题,因为通过打印它,我只能得到第一个 radioButton 的值,跳过所有其他的 ...

我附上截图来更好地解释你

enter image description here

<?php

$servername = "localhost";
$username = "progettocantiere";
$password = "";
$dbname = "my_progettocantiere";

$connessione = mysql_connect("$servername","$username","$password");

if(!$connessione)
{
	die("Errore critico di Connessione al Database" . mysql_error());
}
//connessione
mysql_select_db("$dbname",$connessione);
   
$fkIDCantiere = $_GET["idCantiere"];
$idAffidataria = $_POST["idAffidataria"];
$nomeCantiere =$_POST["nomeCantiere"];
$addettoSicurezza=$_POST["addettoSicurezza"];
$mailAffidataria = $_POST["mailAffidataria"];
$scadenzaCCIAA = $_POST["scadenzaCCIAA"];
$scadenzaDURC = $_POST["scadenzaDURC"];
$contrattoDiAppalto = $_POST["contrattoDiAppalto"];
$pianoDiEmergenzaEAggiornamenti = $_POST["pianoDiEmergenzaEAggiornamenti"];
$CCIAA = $_POST["CCIAA"];
$DURC= $_POST["DURC"];
$DVR= $_POST["DVR"];
$dichiarazioneITP =$_POST["DichiarazioneITP"];
$posRevA = $_POST["posRevA"];
$posRevB = $_POST["posRevB"];
$posRevC = $_POST["posRevC"];
$posInt01 =$_POST["posInt01"];
$registroInfortuni = $_POST["registroInfortuni"];
$nominaRSPP = $_POST["nominaRSPP"];
$attestatoFormazioneRspp = $_POST["attestatoFormazioneRspp"];
$verbaleElezioneRLS = $_POST["verbaleElezioneRLS"];
$attestatoFormazioneRLS = $_POST["attestatoFormazioneRLS"];
$delegaDC = $_POST["DelegaDC"];
$formazioneDirigente = $_POST["formazioneDirigente"];
$nominaPreposto = $_POST["nominaPreposto"];
$formazionePreposto = $_POST["formazionePreposto"];
$nominaAddettoAntincendio =$_POST["nominaAddettoAntincendio"];
$formazioneAddettoAntincendio = $_POST["formazioneAddettoAntincendio"];
$nominaAddettoPS = $_POST["nominaAddettoPS"];
$formazioneAddettoPS = $_POST["formazioneAddettoPS"];
$schedeSicurezzaSostanzeChimiche = $_POST["schedeSicurezzaSostanzeChimiche"];
$nominaMedicoCompetente = $_POST["nominaMedicoCompetente"];
$impiantoElettricoComunicazioneInail = $_POST["impiantoElettricoComunicazioneInail"];
$derogaAlRumore = $_POST["derogaAlRumore"];
$note1 = $_POST["note1"];
$note2= $_POST["note2"]; 
$note3= $_POST["note3"];
$note4= $_POST["note4"];
$note5= $_POST["note5"];
$note6= $_POST["note6"];
$note7= $_POST["note7"];
$note8= $_POST["note8"];
$note9= $_POST["note9"];
$note10= $_POST["note10"];
$note11= $_POST["note11"];
$note12= $_POST["note12"];
$note13= $_POST["note13"];
$note14= $_POST["note14"];
$note15= $_POST["note15"];
$note16= $_POST["note16"];
$note17= $_POST["note17"];
$note18= $_POST["note18"];
$note19= $_POST["note19"];
$note20= $_POST["note20"];
$note21= $_POST["note21"];
$note22= $_POST["note22"];
$note23= $_POST["note23"];
$note24= $_POST["note24"];
$note25= $_POST["note25"];
$note26= $_POST["note26"];
$note27= $_POST["note27"];
$note28= $_POST["note28"];
$note29= $_POST["note29"];
$note30= $_POST["note30"];
$note31= $_POST["note31"];

print_r($_POST);

$ris_news = mysql_query("INSERT INTO Affidataria (idAffidataria,
nomeCantiere,
addettoSicurezza,
mailAffidataria,
scadenzaCCIAA,
scadenzaDURC,
fkIDCantiere,
contrattoDiAppalto,
pianoDiEmergenzaEAggiornamenti,
CCIAA,
DURC,
DVR,
DichiarazioneITP,
posRevA,
posRevB,
posRevC,
posInt01,
registroInfortuni,
nominaRSPP,
attestatoFormazioneRspp,
verbaleElezioneRLS,
attestatoFormazioneRLS,
DelegaDC,
formazioneDirigente,
nominaPreposto,
formazionePreposto,
nominaAddettoAntincendio,
formazioneAddettoAntincendio,
nominaAddettoPS,
formazioneAddettoPS,
schedeSicurezzaSostanzeChimiche,
nominaMedicoCompetente,
impiantoElettricoComunicazioneInail,
derogaAlRumore,
note1,
note2,
note3,
note4,
note5,
note6,
note7,
note8,
note9,
note10,
note11,
note12,
note13,
note14,
note15,
note16,
note17,
note18,
note19,
note20,
note21,
note22,
note23,
note24,
note25,
note26,
note27,
note28,
note29,
note30,
note31)
VALUES ('$idAffidataria',
'$nomeCantiere',
'$addettoSicurezza',
'$mailAffidataria',
'$scadenzaCCIAA',
'$scadenzaDURC',
'$fkIDCantiere',
'$contrattoDiAppalto',
'$pianoDiEmergenzaEAggiornamenti',
'$CCIAA',
'$DURC',
'$DVR',
'$dichiarazioneITP',
'$posRevA',
'$posRevB',
'$posRevC',
'$posInt01',
'$registroInfortuni',
'$nominaRSPP',
'$attestatoFormazioneRspp',
'$verbaleElezioneRLS',
'$attestatoFormazioneRLS',
'$delegaDC',
'$formazioneDirigente',
'$nominaPreposto',
'$formazionePreposto',
'$nominaAddettoAntincendio',
'$formazioneAddettoAntincendio',
'$nominaAddettoPS',
'$formazioneAddettoPS',
'$schedeSicurezzaSostanzeChimiche',
'$nominaMedicoCompetente',
'$impiantoElettricoComunicazioneInail',
'$derogaAlRumore',
'$note1',
'$note2',
'$note3',
'$note4',
'$note5',
'$note6',
'$note7',
'$note8',
'$note9',
'$note10',
'$note11',
'$note12',
'$note13',
'$note14',
'$note15',
'$note16',
'$note17',
'$note18',
'$note19',
'$note20',
'$note21',
'$note22',
'$note23',
'$note24',
'$note25',
'$note26',
'$note27',
'$note28',
'$note29',
'$note30',
'$note31')");

if (!$ris_news) {

	//messaggio di errore per capire il problema
    exit ('<p> Errore mentre recuperavo i dati  ' . mysql_error() . ' </p>');
}
	

/*try
{
 $toInsert1 ="INSERT INTO Affidataria (idAffidataria,nomeAffidataria,addettoSicurezza,mailAffidataria,scadenzaCCIAA,scadenzaDURC) VALUES ('$nomeAffidataria','$addettoSicurezza','$mailAffidataria','$scadenzaCCIAA','$scadenzaDURC')";
 $result1 = mysql_query($toInsert1);
}
catch(Exception $var)
{
  print $var->getMessage();
}
*/
?>
<html>
<body>
<script language="javascript">
  function controlla(){
    x=document.prova;
    if (x.scelta.value=="si"){
      window.location.href = '../subAffidatario.php?idAffidataria=<?php echo $idAffidataria?>'
      return false;
    }
    if (x.scelta.value=="no"){
      alert("Hai risposto no");
       window.location.href = '../affidatario.php'
      return false;
    }
  }
</script>
<fieldset>
<strong>Sei sicuro di voler continuare?</strong>
<form action="?" method="POST" name="prova" onsubmit="return controlla();">
  SI<input type="radio" name="scelta" value="si" /><br />
  NO<input type="radio" name="scelta" value="no" /><br />
  <button type="submit">INVIA</button>
</form>
</fieldset>
</body>
</html>

这是通过插入“print_r($_POST);”返回查询的结果

enter image description here

实际上读取了第一个单选按钮“$contrattoDiAppalto”,并在下一个调用的单选按钮上向我发送了一个错误(如错误所示) "PianoDiEmergenzaEAggiornamenti"

我还向您展示了我用于单选按钮的 html 代码

<html>

<body>
<table>


<tr>
<td bgcolor="#B35556"><form method="POST">
    

       OK <input type="radio" name="contrattoDiAppalto" id="contrattoDiAppalto" value="1" onchange="color(this)" /> <BR>
        NO  <input type="radio" name="contrattoDiAppalto" id="contrattoDiAppalto" value="0" onchange="color(this)" checked/>
       
   
</form></td></tr>
<tr>
<td bgcolor="#B35556">
    <form method="POST">
    
       OK <input type="radio" name="pianoDiEmergenzaEAggiornamenti" id="pianoDiEmergenzaEAggiornamenti" value="1" onchange="color(this)" /> <BR>
       NO <input type="radio" name="pianoDiEmergenzaEAggiornamenti" id="pianoDiEmergenzaEAggiornamenti" value="0" onchange="color(this)" checked/>
       
   </form>
   </td></tr>
   </table>
   </body>
</html>

这一切的寓意:您是如何只阅读第一个无线电按钮而避开所有其他按钮的?

【问题讨论】:

  • 危险:您的查询极易受到 sql 注入的攻击...阅读how-can-i-prevent-sql-injection-in-php
  • 是的,我知道.. 解决这个问题后,我也会纠正这个问题@B001ᛦ
  • 在解决了这个问题后,我也会纠正它...... 听起来像是错误的方法......先解决这个问题,然后再解决其他问题......顺便说一句,你不需要31 $_POST 如果你在上面使用数组
  • 不要使用mysql函数,它已被弃用并删除
  • 哇。我这辈子从没见过这么大的桌子有这么多属性。只是说...

标签: javascript php html mysql database


【解决方案1】:

在您的 html 代码中,您使用了多个标签。这导致提交其中一个表单不会发送其他表单的竞争。因此,您只需删除表单关闭和再次打开即可使其正常工作。

<html>

<body>
<table>


<tr>
<td bgcolor="#B35556"><form method="POST">
    

       OK <input type="radio" name="contrattoDiAppalto" id="contrattoDiAppalto" value="1" onchange="color(this)" /> <BR>
        NO  <input type="radio" name="contrattoDiAppalto" id="contrattoDiAppalto" value="0" onchange="color(this)" checked/>
       
   
</td></tr>
<tr>
<td bgcolor="#B35556">
    
    
       OK <input type="radio" name="pianoDiEmergenzaEAggiornamenti" id="pianoDiEmergenzaEAggiornamenti" value="1" onchange="color(this)" /> <BR>
       NO <input type="radio" name="pianoDiEmergenzaEAggiornamenti" id="pianoDiEmergenzaEAggiornamenti" value="0" onchange="color(this)" checked/>
       
   </form>
   </td></tr>
   </table>
   </body>
</html>

但是请深入研究 html 和 js,因为还有其他“错误”。因此,例如,您在两个 html 元素上使用相同的 id 是不正确的。 “document.getElementById()”只会返回一个随机的(通常是第一个),但这不是它应该的样子。只要您不在 java 脚本代码中使用它们,我建议您删除 id。

【讨论】:

  • 我真的很感谢你的回答..但我不明白一些东西..你是什么意思删除 id?
  • 只需从您的代码中删除 id="contrattoDiAppalto"id="pianoDiEmergenzaEAggiornamenti",我很确定它不会改变任何东西 :-)
猜你喜欢
  • 2014-10-07
  • 2013-01-24
  • 1970-01-01
  • 2012-10-14
  • 2017-04-11
  • 2015-01-18
  • 1970-01-01
  • 2013-10-07
  • 2017-04-27
相关资源
最近更新 更多