简体   繁体   中英

PHP form array into MySQL Database

Ok. It seems that I am having some problems with my PHP code, and it is bugging me. So far, the form half works and half doesn't. here is the code.

<?php
include "cgi-bin/toplinks.php"; include "cgi-bin/charsheetarrays.php"; include "cgi-bin/dropdown.php"; include "cgi-bin/connect_to_mysql.php";

if (isset($_POST['pccharname'])){
    $playerid=$_SESSION['id'];
    $pccharname=ereg_replace("[^A-Z a-z]", "", $_POST['pccharname']);
    $pcclan=$_POST['pcclan'];
    $pcfamily=$_POST['famnames'];
    $pchonor=$_POST['pchonor'];
    $pcglory=$_POST['pcglory'];
    $pcstatus=$_POST['pcstatus'];
    $pctaint=$_POST['pctaint'];
    $charconcept=ereg_replace("[^A-Z a-z]", "", $_POST['charconcept']);
    $pcmon=$_POST['pcmon'];
    $pcfamilyties=ereg_replace("[^A-Z a-z.:]", "", $_POST['pcfamilyties']);
    $pchistorytext=ereg_replace("[^A-Z a-z.:]", "", $_POST['pchistorytext']);
    $pcbelieftext=ereg_replace("[^A-Z a-z.:]", "", $_POST['pcbelieftext'])
    $pcgoalstext=ereg_replace("[^A-Z a-z.:]", "", $_POST['pcgoalstext']);
    $pchookstext=ereg_replace("[^A-Z a-z.:]", "", $_POST['pchookstext']);
    $pcstatagi=$_POST['pcstatagi'];
    $pcstatint=$_POST['pcstatint'];
    $pcstatref=$_POST['pcstatref'];
    $pcstataware=$_POST['pcstataware'];
    $pcstatstam=$_POST['pcstatstam'];
    $pcstatwill=$_POST['pcstatwill'];
    $pcstatstr=$_POST['pcstatstr'];
    $pcstatpercep=$_POST['pcstatpercep'];
    $pcstatvoid=$_POST['pcstatvoid'];
    $pcinitmodroll=$_POST['pcinitmodroll'];
    $pcinitmodkeep=$_POST['pcinitmodkeep'];
    $pcinitmodbonus=$_POST['pcinitmodbonuse'];
    $pcwoundmod=$_POST['pcwoundmod'];
    $pcarmor=$_POST['pcarmor'];
    $pctnmods=$_POST['pctnmods'];
    $pcadddisadlist=$_POST['pcaddisadlist'];
    $ssstringarr=array (ss1=>$_POST['ss0'],ss2=>$_POST['ss1'],ss3=>$_POST['ss2'],ss4=>$_POST['ss3'],ss5=>$_POST['ss4'],ss6=>$_POST['ss5'],ss7=>$_POST['ss6']);
    $ssstring=implode("~",$ssstringarr);

    for($i=0; $i<36; $i++)
    {
        $n=$i-1;
        $skillnum="skill". $n;
        $emphnum="skill". $n ."emph";
        $ranknum="skill". $n ."rank";
        $skillstringarr= array ();
        $skillemphstringarr= array ();
        $skillrankstringarr= array ();
        $skillstringarr[skillnum] = $_POST[skillnum];
        $skillemphstringarr[emphnum] = $_POST[emphnum];
        $skillrankstringarr[ranknum] = $_POST[ranknum];
    }

    $skillstring=implode("~",$skillstringarr);
    $emphstring=implode("~",$skillemphstringarr);
    $rankstring=implode("~",$skillrankstringarr);
    $pctechs=$_POST['pctechs'];
    $pcspells=$_POST['pcspells'];
    $pckata=$_POST['pckata'];
    $pckiho=$_POST['pckiho'];
    $pcworninv=$_POST['pcworninv'];
    $pcownedinv=$_POST['pcownedinv'];
    $pcnormgen=$_POST['pcnormgen'];
    $pcdamgen=$_POST['pcdamgen'];
    $pcgmnotes=$_POST['pcgmnotes'];
    $servinfolog=$_POST['servinfolog'];
    $pcdatesanc=$_POST['pcdatesanc'];
    $pcwhosanc=$_POST['pcwhosanc'];
    $pclastlogin=$_POST['pclastlogin'];
    $pcxpavail=$_POST['pccp'];

    if (($pccharname=="")||($pcclan=="---")||$famname=="---"||$famname=="Pick A Family"||($pcschool=="---")||($pcschool=="--Crab Schools--")||($pcschool=="--Crab Schools--")||($pcschool=="--Crane Schools--")||($pcschool=="--Dragon Schools--")||($pcschool=="--Lion Schools--")||($pcschool=="--Mantis Schools--")||($pcschool=="--Phoenix Schools--")||($pcschool=="--Scorpion Schools--")||($pcschool=="--Unicorn Schools--")||($pcschool=="--Imperial Schools--")||($pcschool=="--Badger Schools--")||($pcschool=="--Dragonfly Schools--")||($pcschool=="--Hare Schools--")||($pcschool=="--Monkey Schools--")||($pcschool=="--Oriole Schools--")||($pcschool=="--Ox Schools--")||($pcschool=="--Sparrow Schools--")||($pcschool=="--Tortoise Schools--")||($pcschool=="--Monk Schools--")||($pcschool=="--Ronin Schools--")||($pcschool=="--Merchant Schools")||($pcschool=="--Pick A School--"))
    {
        $errormsg = "Please correct your error";
    }else{
    $sql=mysql_query("INSERT INTO pcchars (playerid,pccharid,pccharname,pcclan,pcschool,pchonor,pcglory,pcstatus,pctaint,pchistorytext,pcbelieftext,pcgoalstext,pchookstext,charconcept,pcmon,pcfamilyties,pcstatagi,pcstatint,pcstatref,pcstataware,pcstatstam,pcstatwill,pcstatstr,pcstatpercep,pcstatvoid,pcinitmodroll,pcinitmodkeep,pcinitmodbonus,pcwoundmod,pcarmor,pctnmods,pcaddisadlist,ssstring,skillstring,emphstring,rankstring,pctechs,pcspells,pckata,pckiho,pcworninv,pcownedinv,pcgentry,damgentry,pcgmnotes,servinfolog,pcdatecreate,issanced,pcdatesanc,whosanc,pclastlogin,pcxpavail)VALUES('$playerid','$pccharid','$pccharname','$pcclan','$pcschool','$pchonor','$pcglory','$pcstatus','$pctaint','$pchistorytext','$pcbelieftext','$pcgoalstext','$pchookstext','$charconcept','$pcmon','$pcfamilyties','$pcstatagi','$pcstatint','$pcstatref','$pcstataware','$pcstatstam','$pcstatwill','$pcstatstr','$pcstatpercep','$pcstatvoid','$pcinitmodroll','$pcinitmodkeep','$pcinitmodbonus','$pcwoundmod','$pcarmor','$pctnmods','$pcaddisadlist','$ssstring','$skillstring','$emphstring','$rankstring','$pctechs','$pcspells','$pckata','$pckiho','$pcworninv','$pcownedinv','$pcgentry','$damgentry','$pcgmnotes','$servinfolog','now()','$issanced','$pcdatesanc','$whosanc','$pclastlogin','$pcxpavail')") or die (mysql_error());
    }
}else{
echo "<html><head></head><body>Please <a href='http://fiveringsonline.net/login.php'>Login</a> here. You will have to make your character again, because I haven't set it up to save.</body></html>";
}//close id set
?>

Now, for pchistory and the skill,rank, and emph array implodes, i get nothing. also, nothing from adsdissads. now, history and adsdisads are textareas, so, they should technically work.

i understand my for loop my be crap, considering it is passing values around like noone's business.

so, ultimately, i am looking for a bit of help to get this working.

Your variables are not referenced correctly:

$skillstringarr[skillnum] = $_POST[skillnum];
$skillemphstringarr[emphnum] = $_POST[emphnum];
$skillrankstringarr[ranknum] = $_POST[ranknum];

They should be:

$skillstringarr[$skillnum] = $_POST[s$killnum];
$skillemphstringarr[$emphnum] = $_POST[$emphnum];
$skillrankstringarr[$ranknum] = $_POST[$ranknum];

Also note that you are reinitializing your arrays with each loop:

$skillstringarr= array ();
$skillemphstringarr= array ();
$skillrankstringarr= array ();

You should do this once, before the loop, to avoid obliterating data.

Unrelated to the current issue, but relevant nonetheless: Please heed the comments regarding SQL injection.

You need to put the following code OUTSIDE of your for loop.

$skillstringarr= array ();
$skillemphstringarr= array ();
$skillrankstringarr= array ();

Bceause you had this code in your for loop, the array would be reset every time, and you would end up with nothing, which is why your implodes would be empty.

You also need to reference your variables correctly as described in the other answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM