Reputation: 2166
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.
Upvotes: 0
Views: 62
Reputation: 746
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 . '\')');
}
Upvotes: 1
Reputation: 3470
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);
}
?>
Upvotes: 0
Reputation: 5534
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')");
}
?>
Upvotes: 1