14 Replies - 465 Views - Last Post: 11 November 2013 - 10:59 AM Rate Topic: -----

#1 rafa579  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 09-March 13

Relations phpMyAdmin

Posted 09 November 2013 - 02:02 AM

0
down vote
favorite
I have been having trouble with connecting my Database after I learned more or less how to use Relations. What am trying to do is, make users table a relation to players. Am doing a soccer team website and I want the parents to register(which works, thats the users table) and be able to add their kid out database. Here are my Tables.

Users Table

Quote

Column Type Null Default
user_id int(11) No
username varchar(32) No
password varchar(32) No
first_name varchar(32) No
last_name varchar(32) No
email varchar(1024) No
uphone int(65) No
pay_received int(65) No
pregistered int(65) No
active int(11) No 1
type int(1) No 0

Players Table

Quote

Column Type Null Default Links to Comments
p_id int(11) No
p_name varchar(65) No
p_years int(11) No
team_id int(11) No
p_under int(11) No
user_id int(11) No users -> user_id

When I try to add them manually I can, even when it tells me that some items are missing because on the form I only ask for the p_name(name of player) and p_years(years of experience). I try to submit and it does nothing to the players database.

function for the parent to add his son.

function register_c($register_c){

    array_walk($register_c, 'array_sanitize');


$cfields = '`' .implode('`, `', array_keys($register_c)) . '`';
$cdata = '\''. implode('\', \'', $register_c) . '\'';

mysql_query("INSERT INTO players ($cfields, user_id) VALUES ($cdata)");


}

Posting after submit button is pressed.

<?php

         if (isset($_POST['submit']))
        {
            if (empty($_POST) === false && empty($errors) === true) {
            $register_c = array(
            'p_name' => $_POST['p_name'],
            'p_years' => $_POST['p_years']
            ); 

            register_c($register_c);
            exit();

            } else if (empty($errors) === false) {
            echo output_errors($errors);
            }
            }


?>

If anybody could help me that would be great. I don't get how to make the relation when trying to add a player , right now like I said it submits but it is not showed on the database table.

Is This A Good Question/Topic? 0
  • +

Replies To: Relations phpMyAdmin

#2 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3320
  • View blog
  • Posts: 11,227
  • Joined: 12-December 12

Re: Relations phpMyAdmin

Posted 09 November 2013 - 02:43 AM

Aside from anything else, one thing to do is to store the sql-string from here:

mysql_query("INSERT INTO players ($cfields, user_id) VALUES ($cdata)");

into a variable and echo this variable to make sure it is correct. Also copy this echoed string and try to execute it in phpMyAdmin or similar.

But why over-complicate it initially, using array_walk and implode? Get it working first with simple sql-statements.




The mysql library is deprecated, see my signature.
Was This Post Helpful? 0
  • +
  • -

#3 rafa579  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 09-March 13

Re: Relations phpMyAdmin

Posted 09 November 2013 - 04:31 PM

can you elaborate with an example?
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3320
  • View blog
  • Posts: 11,227
  • Joined: 12-December 12

Re: Relations phpMyAdmin

Posted 09 November 2013 - 04:36 PM

$testSql = "INSERT INTO players ($cfields, user_id) VALUES ($cdata)";
echo $testSql;

or use print_r().

I assume that you are not asking me for examples of simple sql-statements :whistling:

This post has been edited by andrewsw: 09 November 2013 - 04:39 PM

Was This Post Helpful? 0
  • +
  • -

#5 rafa579  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 09-March 13

Re: Relations phpMyAdmin

Posted 09 November 2013 - 04:45 PM

woops lol i figured it would be something like that, am sorry, just little stressed out over this. Can't get it to work an its a project due this month.

Nothing came out, I echoed the query and nothing.

This post has been edited by andrewsw: 09 November 2013 - 04:50 PM
Reason for edit:: Removed unnecessary previous quote

Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3320
  • View blog
  • Posts: 11,227
  • Joined: 12-December 12

Re: Relations phpMyAdmin

Posted 09 November 2013 - 04:52 PM

Use print_r($testSql) and view the source for the page.

You might also add this:

error_reporting(E_ALL);
ini_set('display_errors', '1');

to the top of your php so that you can see all errors.
Was This Post Helpful? 0
  • +
  • -

#7 rafa579  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 09-March 13

Re: Relations phpMyAdmin

Posted 09 November 2013 - 09:51 PM

No luck :( the error that it gives me when I try to manually add a player has to do with the user_is since its linked, am not sure how to make it so it generates it to the appropriate I'd

This post has been edited by andrewsw: 10 November 2013 - 01:27 PM
Reason for edit:: Removed previous quote

Was This Post Helpful? 0
  • +
  • -

#8 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3715
  • View blog
  • Posts: 5,972
  • Joined: 08-June 10

Re: Relations phpMyAdmin

Posted 10 November 2013 - 12:08 PM

"INSERT INTO players ($cfields, user_id) VALUES ($cdata)"


Where is the value for the user_id? The $cfields and $cdata values are generated based on an array, so they will be in-sync, but you just add the user_id field there without any value whatsoever.

What value, exactly, should this field be storing? The ID of a logged in user? The ID of some other user? - Where is that value supposed to be coming from? A session? A field in the submitted for? The URL query string perhaps?


Also, while using the obsolete MySQL API functions, you can, and should, catch query errors by storing the return value of mysql_query. In all cases, if it returns FALSE then an error has occured. Use the mysql_error function to get the actual error. Print it, or log it, so you can see what the error is. - Debugging without error messages is like trying to fix a car in total darkness; it's going to be painful, if nothing else.
Was This Post Helpful? 1
  • +
  • -

#9 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3320
  • View blog
  • Posts: 11,227
  • Joined: 12-December 12

Re: Relations phpMyAdmin

Posted 10 November 2013 - 01:29 PM

View Postrafa579, on 10 November 2013 - 04:51 AM, said:

No luck :( the error that it gives me when I try to manually add a player has to do with the user_is since its linked, am not sure how to make it so it generates it to the appropriate I'd

You need to post the complete error details rather than "has to do with..". However, I believe Atli has recognised the main issue.
Was This Post Helpful? 0
  • +
  • -

#10 rafa579  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 09-March 13

Re: Relations phpMyAdmin

Posted 10 November 2013 - 02:02 PM

View PostAtli, on 10 November 2013 - 12:08 PM, said:

"INSERT INTO players ($cfields, user_id) VALUES ($cdata)"


Where is the value for the user_id? The $cfields and $cdata values are generated based on an array, so they will be in-sync, but you just add the user_id field there without any value whatsoever.

What value, exactly, should this field be storing? The ID of a logged in user? The ID of some other user? - Where is that value supposed to be coming from? A session? A field in the submitted for? The URL query string perhaps? ...

The value that I wanted to store is the same ID as the logged user, and yes I do have a error function to show me but in this case it wasnt doing that , I would manually added and it would ask me for the user_id to assign a number to it.Posted Image

This post has been edited by andrewsw: 10 November 2013 - 02:03 PM
Reason for edit:: Reduced quote

Was This Post Helpful? 0
  • +
  • -

#11 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3320
  • View blog
  • Posts: 11,227
  • Joined: 12-December 12

Re: Relations phpMyAdmin

Posted 10 November 2013 - 02:09 PM

You will need to modify your implode method for $cdata so that it also includes the user-id, as currently it results in an empty string. Well you don't need to modify implode(), just to modify $register_c so that it includes the user-id.

You could modify $cdata after the implode, replacing the last '' with the user-id, but this seems messy to me - and it should be simpler to modify $register_c.

This post has been edited by andrewsw: 10 November 2013 - 02:12 PM

Was This Post Helpful? 0
  • +
  • -

#12 rafa579  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 09-March 13

Re: Relations phpMyAdmin

Posted 10 November 2013 - 02:49 PM

View Postandrewsw, on 10 November 2013 - 02:09 PM, said:

You will need to modify your implode method for $cdata so that it also includes the user-id, as currently it results in an empty string. Well you don't need to modify implode(), just to modify $register_c so that it includes the user-id.

You could modify $cdata after the implode, replacing the last '' with the user-id, but this seems messy to me - and it should be simpler to modify $register_c.

that's exactly where am little stuck. Not sure how to call it so it would properly add the user_id that is logged in.
Was This Post Helpful? 0
  • +
  • -

#13 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3320
  • View blog
  • Posts: 11,227
  • Joined: 12-December 12

Re: Relations phpMyAdmin

Posted 10 November 2013 - 04:05 PM

So how is $register_c built? Post the code that does this.

BTW You don't have to keep quoting the previous post, there is a larger Reply button a little further down the page.

This post has been edited by andrewsw: 10 November 2013 - 04:04 PM

Was This Post Helpful? 0
  • +
  • -

#14 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3715
  • View blog
  • Posts: 5,972
  • Joined: 08-June 10

Re: Relations phpMyAdmin

Posted 10 November 2013 - 04:13 PM

View Postrafa579, on 10 November 2013 - 09:02 PM, said:

The value that I wanted to store is the same ID as the logged user,

OK. Where is the ID of the logged in user stored?

In your original code, on line 6 of your controller script (the second one, where you create the data array passed to register_c) the $register_c array is defined. How about just adding the "user_id" to that array there, setting it to the user's ID value? Presumably you have that in the session somewhere?


By the way, just to mention this. Your variable names need work. Why register_c? What is "c" there, exactly? It's not at all clear what this function is meant to register, without diving into the actual function code. - It would be much clearer if you called it something like register_player. That's clear. No question what that is supposed to be doing. - Explicit naming is always better. Trust me, it may not matter in smaller projects with few developers, but it will start to matter as the sizes of the projects, and the sizes of the teams, start to grow. Better get into the habit sooner than later.

View Postrafa579, on 10 November 2013 - 09:02 PM, said:

and yes I do have a error function to show me but in this case it wasnt doing that

I see. However in the code you posted there was no error handling at all.
Was This Post Helpful? 0
  • +
  • -

#15 rafa579  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 09-March 13

Re: Relations phpMyAdmin

Posted 11 November 2013 - 10:59 AM

sorry to answer so late, was little busy with another thing at school. I made it work last night,


Thanks a lot guys! here is the code that worked.

<?php
if (isset($_GET['username=']) && empty($_GET['username='])){
			echo 'Successfully created a Team.';
			}
			else{
				if (empty($_POST) === false && empty($errors) === true) {
				$register_c = array(
				'p_name' => $_POST['p_name'],
				'p_years' => $_POST['p_years'],
				'user_id' => $_POST['user_id']
				); 
				register_c($register_c);
				header('Location: profile.php?username=');
				exit();
				
				} else if (empty($errors) === false) {
				echo output_errors($errors);
				}
				}
?>



			 <form action="" method ="post">
		<ul>           
				   Name of Kid:<br>
				   <input name="p_name" type="text"><br>
				   UTeam: <br>      
					   <input name="p_years" type="text"><br>
					  <br>
					 <INPUT TYPE="hidden" NAME="user_id" value="<?php echo $user_id ?>">
		</p>
	  <p>
					<input type="submit" name="submit" value="submit" />

	  </p><br>
	</form>	

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1