4

I am trying to get what should otherwise be a simple but of php to insert sample data into a table, but something just isn't having any of it.

Table Definition:

CREATE TABLE IF NOT EXISTS teams ( 
     token varchar(12) COLLATE utf8_unicode_ci NOT NULL, 
     tname varchar(48) COLLATE utf8_unicode_ci NOT NULL, 
     captain varchar(64) COLLATE utf8_unicode_ci NOT NULL, 
     email varchar(64) COLLATE utf8_unicode_ci NOT NULL, 
     phone varchar(14) COLLATE utf8_unicode_ci NOT NULL, 
          PRIMARY KEY (token), 
          UNIQUE KEY name (tname), 
          KEY id (token) 
     )  
     ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Code:

$con = mysqli_connect("localhost","username","password","database");

$tname = "Big Bang";
$cname = "Mike";
$cemail = "[email protected]";
$cphone = "123-456-7898";

$teamToken = strtoupper(bin2hex(mcrypt_create_iv(6, MCRYPT_DEV_URANDOM)));

$query = "INSERT INTO teams (token, tname, captain, email, phone) VALUES ('" . $teamToken . "', '" . $tname . "', '" . $cname . "', '" . $cemail . "', '" . $cphone . "')";

if (mysqli_query($con, $query))
{
    echo "Pass!";
}
else
{
    echo $query;
}

mysqli_close($con);

What's odd is the php echos the query, because the mysqli_query result is false, yet the echoed query, when copied and pasted right into phpMyAdmin's terminal, works fine.

I am at my qit's end.

12
  • FYI: I have even tried $query = "INSERT INTO teams (token, tname, captain, email, phone) VALUES ('$teamToken', '$tname', '$cname', '$cemail', '$cphone')"; It produces identical results. Commented Oct 8, 2015 at 9:03
  • 2
    You should print mysqli_error($con) when the query fails so you see the reason for the failure. Commented Oct 8, 2015 at 9:06
  • I have tried that, but mysqli_error($con) returns null. Commented Oct 8, 2015 at 9:09
  • I find that hard to believe. If a query fails, mysqli_error always returns the reason for the failure. Commented Oct 8, 2015 at 9:11
  • I have replaced the original if/else block with mysqli_query($con, $query) or die(mysqli_error($con)); echo mysqli_error($con); Still a blank page. If this helps, the file has 0644 permissions. EDIT: I greatly appreciate your help, btw :) Commented Oct 8, 2015 at 9:13

5 Answers 5

2

Your Code:

$con = mysqli_connect("localhost","username","password","database");

Edited code:

$con = mysqli_connect("localhost","root","","database");
if(!$con):
die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
endif;

Its worked fine on my localhost

Sign up to request clarification or add additional context in comments.

6 Comments

Connect Error (1045) Access denied for user 'mytestuser'@'localhost' (using password: NO) When I put the password in I get Connect Error (1045) Access denied for user 'xsoft16dbu'@'localhost' (using password: YES)
you should change root instead of username. Are you running your code on local?
@MichaelDeLuca You need to add appropriate grants for mytestuser to the database.
I think i should add some background here. This is a Godaddy cPanel shared hosting. I have already went through the wizard to create both the table and the user, and grant the user select and insert access. Beyond that, unless Godaddy's cPanel is pulling me in for a nasty ride, I have no idea why a user I created and granted access still does not have access.
Ok, now I have simply reset the password to one that is only alphanumeric, and retried: Connect Error (1044) Access denied for user 'myuser'@'localhost' to database 'mydb'
|
1

Maybe the datatypes of our columns make any problems.

I have once a similar "error" where my token field was to short. phpMyAdmin simply cut the long string to fit (or some thing this) and so it worked inside phpMyAdmin but not with my program.

Please post the CREATE statement of your table.

2 Comments

CREATE TABLE IF NOT EXISTS teams ( token varchar(12) COLLATE utf8_unicode_ci NOT NULL, tname varchar(48) COLLATE utf8_unicode_ci NOT NULL, captain varchar(64) COLLATE utf8_unicode_ci NOT NULL, email varchar(64) COLLATE utf8_unicode_ci NOT NULL, phone varchar(14) COLLATE utf8_unicode_ci NOT NULL, PRIMARY KEY (token), UNIQUE KEY name (tname), KEY id (token) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
I'm noticing an needed reference to an id key which no longer exists
0

Try this

mysqli_query($con, $query) or die(mysqli_error($con));

2 Comments

This will tell him the reason for the error, it won't solve the problem. It should be a comment.
I replaced the whole if/else block with: mysqli_query($con, $query) or die(mysqli_error($con)); The result was a blank page
0

I will suggest go step by step step 1 : $con = mysqli_connect("localhost","username","password","database"); comment everything other than this statement. then below write

  if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

verify your username password and database name (by default:username=="root" password=="" )

step 2 : add the query statements and print it. also check that all values are within their bounds as specified id DB.

step 3 :if $con and $query are valid then you will get your o/p as you req.

If still you have an error please paste your error statement.

Comments

0

using following to connect:

username="root" password=""

Your code works fine in my system.

I used varchar as datatype. Maybe the length of the varchar which you have taken as 12 is less to store the $teamToken variable. Try increasing the size.

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.