Help - Search - Members - Calendar
Full Version: Sql- Calling An Insert Via Functions
Zymic Webmaster Forums > Web Design & Development > Server Side Scripting > Other Languages
The Seventh Hokage
CODE
<?php
$ss= mysqli_connect($host,$user,$pass,$db) or die("ya did it wrong");//connect to db

function createScroll($x)
{
$xx=$x;
$ssq= "INSERT INTO scrolls(name) VALUES ('Dana')";

$ssres= mysqli_query($xx,$ssq) or die ("You messed xxxx");
}



createScroll($ss);
?>



for some reason i keep getting the error for the query. it can't connect to the DB no matter what i do. do i need to return any info to bring it back to the main program so it can update the DB?

note that i tried it without the function braces and the db still wasnt updated
swordz
CODE
<?php
$ss= mysqli_connect($host,$user,$pass,$db) or die(mysql_error());//connect to db

function createScroll($x)
{
$xx=$x;
$ssq= "INSERT INTO scrolls(name) VALUES ('Dana')";

$ssres= mysqli_query($xx,$ssq) or die (mysql_error());
}
createScroll($ss);
?>


Try that code - you'll actually get an error that's useful to us.

However, I suspect the problem is the missing space between scrolls and name.

Swordz
The Seventh Hokage
okay. thanks to that error checking code segment i fixed the code and it works. but thats simply one step in my plan.

my original intent is to have the code behave differently based on a switch case... like so..

CODE
function createScroll($x)
{
$xx=$x;
switch($_SESSION[scrtype])
{
case "Non":

$ssq= "INSERT INTO scrolls (name,effect,non,fire,wind,water,earth,description,quantity) VALUES (SESSION[scrname],$_SESSION[scrvalue],1,0,0,0,0,$_SESSION[scrdesc],1)";

$ssres= mysqli_query($xx,$ssq) or die (mysql_error());
break;


case "Fire":

$ssq= "INSERT INTO scrolls (name,effect,non,fire,wind,water,earth,description,quantity) VALUES (SESSION[scrname],$_SESSION[scrvalue],0,1,0,0,0,$_SESSION[scrdesc],1)";

$ssres= mysqli_query($xx,$ssq) or die (mysql_error());
break;


case "Wind":

$ssq= "INSERT INTO scrolls (name,effect,non,fire,wind,water,earth,description,quantity) VALUES (SESSION[scrname],$_SESSION[scrvalue],0,0,1,0,0,$_SESSION[scrdesc],1)";

$ssres= mysqli_query($xx,$ssq) or die (mysql_error());
break;


case "Water":

$ssq= "INSERT INTO scrolls (name,effect,non,fire,wind,water,earth,description,quantity) VALUES (SESSION[scrname],$_SESSION[scrvalue],0,0,0,1,0,$_SESSION[scrdesc],1)";

$ssres= mysqli_query($xx,$ssq) or die (mysql_error());
break;


case "Earth":

$ssq= "INSERT INTO scrolls (name,effect,non,fire,wind,water,earth,description,quantity) VALUES (SESSION[scrname],$_SESSION[scrvalue],0,0,0,0,1,$_SESSION[scrdesc],1)";

$ssres= mysqli_query($xx,$ssq) or die (mysql_error());
break;
}
}



when i fixed the code from yesterday it worked fine . i confirmed inserted data into my DB.

but this code does not work after adding the switch case in.... did i implement it wrong?
swordz
The SQL comes first - it should be mysqli_query($ssq.$xx). I'm surprised this worked yesterday...

Swordz
The Seventh Hokage
that causes an error that says "Warning: mysqli_query() expects parameter 1 to be mysqli, string given in"

starting with the first SQL statement of the switch case.

the other way worked.. but for some reason i just can't get it to work with a switch statement.

perhaps it's because of multiple connections? i do have another connection open on the page.
swordz
What errors are you getting?

Swordz

EDIT: Just seen it: (SESSION[scrname], - no $ - also, it won't like the [] part. Try breaking the string and concatenating - it's faster too.
The Seventh Hokage
i'm not sure what you mean by breaking the string apart.

im not even sure WHY sql considers the connection a string considering i write it just like every other connection i've ever written and they all work......
swordz
In PHP if you have something in " or ' that's a string.

I mean: $ssq= "INSERT INTO `scrolls` (`name`,`effect`,`non`,`fire`,`wind`,`water`,`earth`,`description`,`quantity`) VALUES ('" . $_SESSION['scrname'] . "','" . $_SESSION['scrvalue'] . "','0','0','0','0','1','" . $_SESSION['scrdesc'] . "','1')";

Note: ` around table and column names, ' around variables, ' around array keys. All are good practice (especially the array key ' - you've been relying on PHP error handling).

Swordz
The Seventh Hokage
ah okay. i am enlightened. i took a small break from coding yesterday. i've been at this particular project for almost two months straight so ya. anyway thanks
kobe james
rolleyes.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2013 Invision Power Services, Inc.