Sql- Calling An Insert Via Functions, not working |
||
Welcome to the Zymic webmaster forums. Our forums are here to provide people the free ability to discuss a range of websites related topics such as design, development coding and marketing.
In order to post you will need to register for a zymic account or if you already have one simply login by using the form on the left.
Zymic Webmaster Forums Web Design & Development Server Side Scripting Other Languages |
||
![]() |
Sql- Calling An Insert Via Functions, not working |
||
Sep 27 2009, 07:11 PM
Post
#1
|
|
![]() Member ![]() ![]() Group: Members Posts: 90 Joined: 29-May 09 Member No.: 97,073 |
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 |
|
|
Sep 27 2009, 10:19 PM
Post
#2
|
|
|
Outrageously Uber Ninja ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Moderators Posts: 1,805 Joined: 10-July 08 From: UK Member No.: 44,994 |
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 |
|
|
Sep 28 2009, 08:54 PM
Post
#3
|
|
![]() Member ![]() ![]() Group: Members Posts: 90 Joined: 29-May 09 Member No.: 97,073 |
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? |
|
|
Sep 29 2009, 12:43 AM
Post
#4
|
|
|
Outrageously Uber Ninja ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Moderators Posts: 1,805 Joined: 10-July 08 From: UK Member No.: 44,994 |
The SQL comes first - it should be mysqli_query($ssq.$xx). I'm surprised this worked yesterday...
Swordz |
|
|
Sep 29 2009, 04:20 PM
Post
#5
|
|
![]() Member ![]() ![]() Group: Members Posts: 90 Joined: 29-May 09 Member No.: 97,073 |
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. |
|
|
Sep 29 2009, 05:47 PM
Post
#6
|
|
|
Outrageously Uber Ninja ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Moderators Posts: 1,805 Joined: 10-July 08 From: UK Member No.: 44,994 |
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. |
|
|
Sep 30 2009, 01:13 AM
Post
#7
|
|
![]() Member ![]() ![]() Group: Members Posts: 90 Joined: 29-May 09 Member No.: 97,073 |
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...... |
|
|
Sep 30 2009, 09:21 AM
Post
#8
|
|
|
Outrageously Uber Ninja ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Moderators Posts: 1,805 Joined: 10-July 08 From: UK Member No.: 44,994 |
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 |
|
|
Oct 1 2009, 01:07 PM
Post
#9
|
|
![]() Member ![]() ![]() Group: Members Posts: 90 Joined: 29-May 09 Member No.: 97,073 |
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
|
|
|
Feb 1 2012, 06:59 AM
Post
#10
|
|
|
Newbie ![]() Group: Members Posts: 9 Joined: 1-February 12 From: UK Member No.: 228,443 |
|
|
|
![]() |
2 User(s) are reading this topic (2 Guests and 0 Anonymous Users) |
||
| 0 Members: | ||
Forum Jump |
||
| Lo-Fi Version | Time is now: 24th May 2013 - 07:26 AM |