Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
195 views
in Technique[技术] by (71.8m points)

php - How to store an array into a table?

I'm trying to store an array into a table but its not working it adds the table but it doesn't add the column name at all. It's just empty

Here's the entire code.

<?php
include 'db.php';

  if(isset($_GET['NAME'])) {
  $sector = mysql_real_escape_string($_GET['SECTORPOSITION']) ; // escape your variable here .
  $name = mysql_real_escape_string($_GET['NAME']) ; // escape your variable here .
  mysql_query("INSERT INTO $sector  (Name) VALUES ('$name') ") or die(mysql_error()) ;
}

    if(isset($_GET['TYPE'])) {
    file_put_contents('contents.txt', $_GET['TYPE'] . "
", FILE_APPEND);
    }

    if(isset($_GET['ISEXPLORED'])) {
    file_put_contents('contents.txt', $_GET['ISEXPLORED'] . "
", FILE_APPEND);
    }

    if(isset($_GET['SECTORPOSITION'])) {
        mysql_query("CREATE TABLE `".$_GET['SECTORPOSITION']."` ( Name VARCHAR(30), Type VARCHAR(30), IsExplored VARCHAR(30), SectorPosition VARCHAR(30), guid VARCHAR(30))");
    }

    if(isset($_GET['GUID'])) {
    file_put_contents('contents.txt', $_GET['GUID'] . "
", FILE_APPEND);
    }

    print('Added!');
?>

'RESOLVED THANKS TO ECHO' 'move the code of creating table first then insert to that table. you are inserting then creating table , you should do the opposite.'

Problem 2

Hey guys. I'm having an issue when I do

/test/test.php?SECTORPOSITION=13137&NAME=hibb&TYPE=Cluster&ISEXPLORED=true&GUID=13 I get a syntax error.

But when I do

?SECTORPOSITION=hey&NAME=hibb&TYPE=Cluster&ISEXPLORED=true&GUID=13 It works fine?

Here's my code.

<?php
include 'db.php';
    if(isset($_GET['SECTORPOSITION'])) {
        mysql_query("CREATE TABLE `".$_GET['SECTORPOSITION']."` ( Name INT, Type VARCHAR(30), IsExplored VARCHAR(30), SectorPosition INT, guid INT)");
    }


  if(isset($_GET['TYPE'])) {
  $sector = mysql_real_escape_string($_GET['SECTORPOSITION']) ; // escape your variable here .
  $type= mysql_real_escape_string($_GET['TYPE']) ; // escape your variable here .
  $name = mysql_real_escape_string($_GET['NAME']) ; // escape your variable here .
  $isexplored = mysql_real_escape_string($_GET['ISEXPLORED']) ; // escape your variable here 
  $guid = mysql_real_escape_string($_GET['GUID']) ; // escape your variable here 
  mysql_query("INSERT INTO $sector  (Name,Type,IsExplored,SectorPosition,guid) VALUES ('$name','$type','$isexplored','$sector','$guid') ") or die(mysql_error()) ;
}
    print('Added!');
?>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you can do like this

look like I have an array

$array = array(1,2,3,4);

json_encode($array);

and save the json encoded value

Its not standard to store array in db. You could see any cms, they would store it as json encoded objects, so that they can retrieve back the values


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...