1

Hey I am currently trying to insert a global variable to a table. The other values I pass are variables too but they get sent correctly.

Here is my query. my error handling does not capture anything

$result = mysql_query("INSERT INTO IPmanagement (userId, NameUsed, EmailUsed, IPStatus, Ip) VALUES ('" .$masterUserId . "', '" . $Entry['LeadName'] . "', '" . $Entry['LeadEmail'] . "', '0', '" . $ip . "')") or die(ErrorException("Function 6", "Error when processing the current lead. your data is unaffected and if the proccess continues please contact an Admin.", mysql_error(),$_SERVER['REMOTE_ADDR'], CurrentPath(), $masterUserId));

my variable that is global defined before the function is

 $masterUserId = "1";

I tried echoing the variable before it sends and it echos out correctly YET my table holds a value of 0.

here is a screenshot of how I have my table setup.

enter image description here

Click for Larger Image

Any idea what is going on. I am rather stumped and tried writing this same code different ways and it still gives me same issue. Also $masterUserId will always be an int value

Edit: also would like to mention the variable is different .php that contains the varaiable and database login information. It is being included at the top. (don't know if that is relevant)

4
  • 1
    Do not use mysql_*. Replace them with mysqli_* or PDO::. Did you try to echo the mysql_query()? Do this. Replace mysql_query("..."); with die("..."); and put it in the phpMyAdmin and try executing. Commented Oct 4, 2012 at 5:58
  • I don't think this is a PHP error. It must be a logical error. Try echoing $masterUserId just before executing the query ! Commented Oct 4, 2012 at 6:03
  • @GauravSrivastava The OP said he did it. Please read the question again! Commented Oct 4, 2012 at 6:08
  • Hi @EmanuelPermane, if any of below answers has solved your question please consider accepting the best answer or adding your own solution. So, that it indicates to the wider community that you've found a solution. Commented Jan 4, 2017 at 3:24

3 Answers 3

1

Because you are not inserting IP STATUS.Which is not null

DATABASE SCHEMA

\

You should either set this to null or enter some value to it.

If you are using query in a function than use like this

function (){
//than define 
 $globat $masterUserId;
// use the global defination 
// than use this variable with global value
}
Sign up to request clarification or add additional context in comments.

7 Comments

Good call, I added it but it still shows userId as 0. I am editing the original post to reflect that
@EmanuelPermane may be $masterUserId = 1; can help you try this.
@EmanuelPermane are you using defining its global defination like function (){ global $masteUserId; //than code... }
No i was declaring it outside of any function and then that file was included into this one.
It gives me Parse error: syntax error, unexpected T_VARIABLE in XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX on line 166. I put the golbat in the same function i'm importing correct?
|
0

Do not use mysql_*. Replace them with mysqli_* or PDO::.

Did you try to echo the mysql_query()? Do this. Replace mysql_query("..."); with die("..."); and put it in the phpMyAdmin and try executing.

And in your table, I see that IP Status is a NOT NULL. So that might throw an exception. Use a default value in the table.

And yeah, what do you get the result as in mysql_error()?

1 Comment

It gives you an output on what query will be executed, so that you can see if that was the required query.
0

Why ''' or "' in query? I have cleaned up query with PHP function sprintf and using NULL for EntryID(Autoincrement)

$query = sprintf("INSERT INTO IPmanagement (EntryID,userId, NameUsed, EmailUsed, IPStatus, Ip) VALUES (NULL,%s,%s,%s,'0',%s)",
$masterUserId , $Entry['LeadName'] , $Entry['LeadEmail'] , $ip ));
$result = mysql_query($query);

You should also use MySQLi or PDO

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.