0

I have created a set of usernames. Usernames and passwords are same. I want to insert these names into my table using for loop.

<?php
$userltr="sys";
$usernames=array();
include('connection.php');
for($i=1;$i<=100;$i++)
{
    $usernames[$i-1]=$userltr.$i;
    //mysql_query("insert into student_login values('$usernames','$usernames')");
}

?>

Also if i put echo"\n" below the $usernames line it shows the same error.

3
  • values('$usernames','$usernames')") should be values('".$usernames[$i-1]."','".$usernames[$i-1]."',)") as you want to insert the specific value, not the entire array. Commented Mar 30, 2016 at 22:15
  • Where is the original code that the answers are related to? Why did you edit your question to fix the error? Commented Mar 30, 2016 at 22:48
  • The way that is written is vulnerable to sql injection (very easy to hack.) Use prepared statements. Commented Mar 31, 2016 at 0:20

3 Answers 3

1

Your problem is with variable $usernames which is array due to this string:

$usernames[$i-1]=$userltr.$i;

Try to change your code to this:

<?php
$userltr="sys";
$usernames=array();
include('connection.php');
for($i=1;$i<=100;$i++)
{
    $username = $userltr.$i;
    $usernames[$i-1]=$username;
    mysql_query("insert into student_login values('$username','$username')");
}
?>
Sign up to request clarification or add additional context in comments.

2 Comments

Query successfully executed but the values in the table is Array. Like all the fields are array.
Are you really create variable $username = $userltr.$i; and insert it with this query mysql_query("insert into student_login values('$username','$username')");?
1

Try something like this:

$users = array('Mark', 'John', 'Luke', 'Brogan');
foreach($users as $user) {
    mysql_query('INSERT INTO student_login (username) VALUES (\'' . mysql_real_escape_string($user) . '\')');
}

To include passwords...

$users = array(
    array('Mark', 'markymark'),
    array('John', 'ilovelucy'),
    array('Luke', '1234567'),
    array('Brogan', '!SJ4vkxaH95Smb^2')
);

foreach($users as $key => $value) {
    mysql_query('INSERT INTO student_login (username, password) VALUES (\'' . mysql_real_escape_string($value[0]) . '\', \'' . mysql_real_escape_string($value[1]) . '\')');
}

Not too sure what the deal is with your $userltr variable, but it looks like you might be trying to prefix each username with 'sys'.

foreach($users as $key => $value) {
    mysql_query('INSERT INTO student_login (username, password) VALUES (\'' . mysql_real_escape_string('sys' . $value[0]) . '\', \'' . mysql_real_escape_string($value[1]) . '\')');
}

Inserting into the database where the password is identical to the username:

$users = array('Mark', 'John', 'Luke', 'Brogan');
foreach($users as $user) {
    mysql_query('INSERT INTO student_login (username, password) VALUES (\'' . mysql_real_escape_string($user) . '\', \'' . mysql_real_escape_string($user) . '\')');
}

Barmar thinks this is what you're trying to do:

<?php
include('connection.php');

for($i = 1; $i <= 100; ++$i) {
    $username = mysql_real_escape_string('sys' . $i);
    mysql_query('INSERT INTO student_login (username, password) VALUES (\'' . $username . '\', \'' . $username . '\')');
}

8 Comments

He's just trying to generate usernames sys1, sys2, sys3, etc.
Whats the point of creating a list of usernames, only to overwrite all those usernames and never use them?
Where is he overwriting them? He's creating the list of usernames, and inserting them into the DB. But it looks like he's edited the question since you posted your answer, so maybe that's why I'm confused.
Ahhhh its just a mess. I've added a solution based on your comment and the changes he's made to his question.
@Barmar all set, And you are ryt about what i want. last edit was the originals.
|
0

Try this:

<?php
$userltr="sys";
$usernames=array();
include('connection.php');
for($i=0;$i<100;$i++)
{
    $usernames[$]=$userltr; // or $usernames[$]= $userltr .= $1;
}

foreach($usernames as $item){
mysql_query("insert into student_login values($item,$item);
}


?>

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.