Jump to content

Recommended Posts

Hey all,

 

i have a multi table instert i am trying to do and its not inserting and is only displaying blank could i get a second set of eyes to check the code and tell me where i went wrong? i am using MySQL 4.1.22

 

<?php
include './config.php'; //(holds the actual database info and some variables for tables (that im not actually using))
//include './access.php'; //(Holds some array variables that i use in other pages.)


$db3 = mysql_connect ($hostname, $username, $password) or die ('Failed to connect to database: ' . mysql_error());
mysql_select_db ($database);

	$authid = "'" . mysql_real_escape_string($_POST['authid']) . "'";
	$name = "'" . mysql_real_escape_string($_POST['name']) . "'"; 
	$email = "'" . mysql_real_escape_string($_POST['email']) . "'";
	$fid = "'" . mysql_real_escape_string($_POST['fid']) . "'";
	$rank = "'" . mysql_real_escape_string($_POST['rank']) . "'";
	$access = "'" . mysql_real_escape_string($_POST['access']) . "'";
	$admin_id = "'" . mysql_real_escape_string($_POST['admin_id']) . "'";
	$group_id = "'" . mysql_real_escape_string($_POST['group_id']) . "'";

$query = "INSERT INTO bioclan.clan_members a INNER JOIN bioclan.admins b ON a.authid = b.auth INNER JOIN bioclan.sm_admins c ON a.authid = c.identity INNER JOIN bioclan.sm_groups d ON d.id = e.group_id INNER JOIN bioclan.sm_admins_groups e ON c.id = e.admin_id (a.authid,a.name,a.email,a.fid,a.rank,b.auth,b.name,b.access,c.identity,c.name,e.admin_id,e.group_id) VALUES ($authid,$name,$email,$fid,$rank,$authid,$name,$access,$authid,$name,$admin_id,$group_id)";


if ($result = mysql_query($query)) 
{  
	 if (mysql_affected_rows() == 1) 
	{    
		echo "<font size=\"+1\" color=\"red\">Update OK!</font>"; 
		mysql_close($db3);
		include ('./includes/spry.php');
	} 
	else 
	{    
		echo "<font size=\"+1\" color=\"red\">Oops! Something went wrong</font>";
		mysql_close($db3);
		include ('./includes/spry.php');
	}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/193171-mysql-insert-not-working/
Share on other sites

here are the table structures, i hope that i have provided enough information.

 

MySQL 4.1.22

 

clan_members table (a)

+-------------+--------------+------+-----+---------+-------+
| Field       | Type         | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| authid      | varchar(36)  |      | PRI |         |       |
| rank        | varchar(33)  | YES  |     | NULL    |       |
| name        | varchar(33)  | YES  |     | NULL    |       |
| email       | varchar(255) | YES  |     | NULL    |       |
| fid         | varchar(255) | YES  |     | NULL    |       |
| avatar      | varchar(255) | YES  |     | NULL    |       |
| private_pin | varchar(255) |      |     |         |       |
+-------------+--------------+------+-----+---------+-------+

 

admins table (b)

+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| auth     | varchar(32) |      | PRI |         |       |
| name     | varchar(32) |      |     |         |       |
| password | varchar(32) |      |     |         |       |
| access   | varchar(32) |      |     |         |       |
| flags    | varchar(32) |      |     | ce      |       |
+----------+-------------+------+-----+---------+-------+

 

sm_admins table ©

+----------+---------------------------+------+-----+---------+----------------+
| Field    | Type                      | Null | Key | Default | Extra          |
+----------+---------------------------+------+-----+---------+----------------+
| id       | int(10) unsigned          |      | PRI | NULL    | auto_increment |
| authtype | enum('steam','name','ip') |      |     | steam   |                |
| identity | varchar(65)               |      |     |         |                |
| password | varchar(65)               | YES  |     | NULL    |                |
| flags    | varchar(30)               |      |     |         |                |
| name     | varchar(65)               |      |     |         |                |
| immunity | int(10) unsigned          |      |     | 0       |                |
+----------+---------------------------+------+-----+---------+----------------+

 

sm_groups table (d)

+----------------+------------------+------+-----+---------+----------------+
| Field          | Type             | Null | Key | Default | Extra          |
+----------------+------------------+------+-----+---------+----------------+
| id             | int(10) unsigned |      | PRI | NULL    | auto_increment |
| flags          | varchar(30)      |      |     |         |                |
| name           | varchar(120)     |      |     |         |                |
| immunity_level | int(1) unsigned  |      |     | 0       |                |
+----------------+------------------+------+-----+---------+----------------+

 

sm_admins_groups table (e)

+---------------+------------------+------+-----+---------+-------+
| Field         | Type             | Null | Key | Default | Extra |
+---------------+------------------+------+-----+---------+-------+
| admin_id      | int(10) unsigned |      | PRI | 0       |       |
| group_id      | int(10) unsigned |      | PRI | 0       |       |
| inherit_order | int(10)          |      |     | 0       |       |
+---------------+------------------+------+-----+---------+-------+

 

the query

<?php

$query = "INSERT INTO bioclan.clan_members a 
INNER JOIN bioclan.admins b ON a.authid = b.auth 
INNER JOIN bioclan.sm_admins c ON a.authid = c.identity 
INNER JOIN bioclan.sm_groups d ON d.id = e.group_id 
INNER JOIN bioclan.sm_admins_groups e ON c.id = e.admin_id 
(a.authid,a.name,a.email,a.fid,a.rank,b.auth,b.name,b.access,c.identity,c.name,e.admin_id,e.group_id) 
VALUES 
($authid,$name,$email,$fid,$rank,$authid,$name,$access,$authid,$name,$admin_id,$group_id)";
?>

The syntax of your INSERT query is not valid. The only current way of getting mysql to perform a multi-table insert would be to set up a TRIGGER to insert data into the dependent tables - http://dev.mysql.com/doc/refman/5.1/en/faqs-general.html#qandaitem-23-1-1-3

ok i changed my query to this

 

<?php
$query = "INSERT INTO bioclan.clan_members (authid,name,email,fid,rank) VALUES ($authid,$name,$email,$fid,$rank);";
$query .= "INSERT INTO bioclan.admins (auth,name,access) VALUES ($authid,$name,$access);";
$query .= "INSERT INTO bioclan.sm_admins (identity,name) VALUES ($authid,$name);";
?>

 

and it is still doing the same thing, so i echoed $query and copied it into MySQL Console and it worked, im not sure but i am positive there are more ways than just using triggers, atleast i hope so ... also i just read up on that link about triggers and i am already lost.. ..

 

the echo looks like this btw ...

INSERT INTO bioclan.clan_members (authid,name,email,fid,rank) VALUES ('STEAM_0:0:000000','BOB','bob@email.com','000000000000','9');INSERT INTO bioclan.admins (auth,name,access) VALUES ('STEAM_0:0:000000','BOB','abcdefghijklmnopqrstu');INSERT INTO bioclan.sm_admins (identity,name) VALUES ('STEAM_0:0:000000','BOB');INSERT INTO bioclan.sm_admins_groups (admin_id,group_id) VALUES ('65','6')

You must execute each query individually, for a couple of reasons -

 

1) You would want to use error checking, error reporting, and error recovery logic on each query and

2) mysql_query() does not support multiple queries separated by semi-colons ; because too many 'programmers' are not validating and escaping their data to prevent sql injection.

 

Executing multiple queries manually against the database is a special case where a human is involved in the process and he can address any errors that occur.  A program attempting to do the same thing must be bullet proof so that an error in one step does not carry through and insert invalid data in the following steps.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.