I am struggling to create multiple user&password and insert it in mysql column.
inserting multiple username is working but not multiple random password.
mysql table "users" - columns 'user' & 'password' primary key = user column
How it works:
A user enters a username and numbers (how many user & pass to create) in an HTML form.
The code uses the submitted username and adds a serial from "1" to limit (submitted number).
Example input:
submitted user is john
submitted numbers is 20
Example result:
john1, john2... john20
In the future when the user requests another 10 user & pass with same name "john" the number will start from 21 (john21, john22... john30)
adding another series is not done yet any help and tips are welcome.
My code:
function muser() {
function randomPassword() {
$alphabet = "abcdefghjkmnpqrstuwxyz23456789"; // skip 0OoIl1
$pass = array();
$alphaLength = strlen($alphabet) - 1;
for ($i = 0; $i < 6; $i++) {
$n = rand(0, $alphaLength);
$pass[] = $alphabet[$n];
}
return implode($pass);
}
if(isset($_REQUEST['submit'])) {
$user_input_user = $_REQUEST["user"];
$user_input_limit = $_REQUEST["limit"];
$ipass = randomPassword();
}
$uiuser = $user_input_user;
$uilimit = $user_input_limit;
$limit = $uilimit;
for($x = 1; $x <= $limit; $x++) {
$queryuser[] = "('$uiuser$x', '$ipass'),";
}
return implode($queryuser);
}
$mquery = 'INSERT INTO `users` (`user`, `pass`) VALUES ';
$imuser = muser();
$fquery = substr($imuser, 0, -1);
$sql = $mquery . $fquery .';';
The problem is caused because you give value to $ipass only once.
change the for loop from this
for($x = 1; $x <= $limit; $x++) {
$queryuser[] = "('$uiuser$x', '$ipass'),";
}
to
for($x = 1; $x <= $limit; $x++) {
$queryuser[] = "('$uiuser$x', '$ipass'),";
$ipass = randomPassword();
}
Related
I'm trying to save arrays on a MySQL database with PHP.
The code inserts only the first line, if I have an array of 5 elements it just inserts the first element and the others and 4 don't save them for me.
Can anyone tell me where I'm wrong?
Thanks a lot.
<?php
//getting user values
$day = $_POST['Day'];
$nDay = $_POST['n_Day'];
$fieldOne = $_POST['Field_one'];
$fieldTwo = $_POST['Field_two'];
$timeOne = $_POST['Time_one'];
$timeTwo = $_POST['Time_two'];
$idR = $_POST['id_ristorante'];
$day_array = explode(",",$day);
$nDay_array = explode(",",$nDay);
$timeOne_array = explode(",",$timeOne);
$timeTwo_array = explode(",",$timeTwo);
$len = count($day_array and $nDay_array and $timeOne_array and $timeTwo_array);
$output=array();
//require database
require_once('db.php');
//checking if email exists
$conn=$dbh->prepare('SELECT id_ristorante FROM Orari WHERE id_ristorante=:idR');
$conn->bindParam(':idR', $idR, PDO::PARAM_STR);
$conn->execute();
//results
if($conn->rowCount() !==0){
$output['isSuccess'] = 0;
$output['message'] = "Orario già inserito";
} else {
for($i=0;$i<$len;$i++){
$day = $day_array[$i];
$nDay = $nDay_array[$i];
$timeOne = $timeOne_array[$i];
$timeTwo = $timeTwo_array[$i];
$conn=$dbh->prepare('INSERT INTO Orari (Day, n_Day, Field_one, Field_two, Time_one, Time_two, id_ristorante) VALUES (?,?,?,?,?,?,?)');
//encrypting the password
$conn->bindParam(1,$day);
$conn->bindParam(2,$nDay);
$conn->bindParam(3,$fieldOne);
$conn->bindParam(4,$fieldTwo);
$conn->bindParam(5,$timeOne);
$conn->bindParam(6,$timeTwo);
$conn->bindParam(7,$idR);
$conn->execute();
if($conn->rowCount() == 0) {
$output['isSuccess'] = 0;
$output['message'] = "Errore, riprova.";
} elseif($conn->rowCount() !==0){
$output['isSuccess'] = 1;
$output['message'] = "Orari salvati!";
}
}
}
echo json_encode($output);
?>
When you trying to do count on multiple array as:
$len = count($day_array and $nDay_array and $timeOne_array and $timeTwo_array);
The and cause the array to be boolean evaluated so the final assign to $len is 1 and that why the loop is done only once and only the first element is inserted to DB.
If all the array in the same length you should just do count on 1 of them as:
$len = count($day_array)
Better practice will be to do count on each on them and then assign $len with the minimum value
Change this line
$len = count($day_array and $nDay_array and $timeOne_array and $timeTwo_array);
To
$len = count($day_array) + count($nDay_array) + count($timeOne_array) + count($timeTwo_array);
Up until now I've always been limiting my pages for any pagination simply using MySQL LIMIT. It's been performing well.
However... Now I've written an application that grabs the data from 3 separate MySQL tables, after that I'm using array_multisort() to sort them.
The main issue here is that it is used for SMS messages(gammu smsd). If I write a message consisting of let's say 900 characters - it is split to 7 entries in the database. It is split every 139 characters (the number might be different sometimes)
Below code somewhat fixes this issue based on 'SequencePosition' field in the database.
So if I were to LIMIT it using MySQL I would have less results than I specified since MySQL has no idea what I'm doing with the data later.
I hope it makes sense to you guys.
How would you solve this issue? Can you think of a better way to do it? I mean- grabbing all the data from the table is rarely a good idea ;(
See PHP code below.
<?php
if(isset($_SESSION['id']))
{
if(isset($_GET['contact_number']) && ($_GET['contact_name']))
{
if (isset($_GET["page"]))
{
$page = $_GET["page"];
}
else
{
$page = 1 ;
}
$gmessages_page = 30; //this is actually a static value, normally it resides in a config file
$start_from = ($page-1) * $gmessages_page;
$contact_number = base64_decode($_GET['contact_number']);
$contact_name = base64_decode($_GET['contact_name']);
$query_inbox = "SELECT ReceivingDateTime, SenderNumber, TextDecoded FROM inbox WHERE SenderNumber='$contact_number' ORDER BY ReceivingDateTime";
$query_sentitems = "SELECT SendingDateTime, DestinationNumber, TextDecoded, Status, SequencePosition FROM sentitems WHERE DestinationNumber='$contact_number' ORDER BY SendingDateTime";
$query_outbox = "SELECT SendingDateTime, DestinationNumber, TextDecoded FROM outbox WHERE DestinationNumber='$contact_number' ORDER BY SendingDateTime";
$result_inbox = $conn->query($query_inbox);
$result_sentitems = $conn->query($query_sentitems);
$result_outbox = $conn->query($query_outbox);
if(!$result_inbox || !$result_sentitems || !$result_outbox)
{
die("Błąd połączenia z bazą (!RESULT)");
}
$rows_inbox = $result_inbox->num_rows;
$rows_sentitems = $result_sentitems->num_rows;
$rows_outbox = $result_outbox->num_rows;
if($rows_inbox == 0 || $rows_sentitems == 0)
{
$conn->close();
}
//inbox
$inbox_person = $contact_name;
for($j = 0; $j < $rows_inbox ; ++$j)
{
$result_inbox->data_seek($j);
$row_inbox = $result_inbox->fetch_array(MYSQLI_NUM);
$inbox_date[] = $row_inbox[0];
$inbox_number = $row_inbox[1];
$inbox_text[] = $row_inbox[2];
$sent_status[] = 0;
$sent_sequence_position[] = 0;
$inbox_type[] = 1;
}
for($j = 0; $j < $rows_sentitems ; ++$j)
{
$result_sentitems->data_seek($j);
$row_sentitems = $result_sentitems->fetch_array(MYSQLI_NUM);
if($row_sentitems[4] == 1)
{
$sent_sequence_position[] = $row_sentitems[4];
$inbox_date[] = $row_sentitems[0];
$inbox_text[] = $row_sentitems[2];
$sent_status[] = $row_sentitems[3];
$inbox_type[] = 2;
}
else
{
$inbox_text[sizeof($inbox_type) - 1] .= $row_sentitems[2];
}
}
for($j = 0; $j < $rows_outbox ; ++$j)
{
$result_outbox->data_seek($j);
$row_outbox = $result_outbox->fetch_array(MYSQLI_NUM);
$inbox_date[] = $row_outbox[0];
$inbox_text[] = $row_outbox[2];
$sent_status[] = "QueuedForSending";
$inbox_type[] = 2;
}
$number_of_records = sizeof($inbox_date);
$number_of_pages = ceil($number_of_records / $gmessages_page);
array_multisort($inbox_date, $inbox_text, $inbox_type, $sent_status, $sent_sequence_position);
$smarty->assign("inbox_date", $inbox_date);
$smarty->assign("inbox_text", $inbox_text);
$smarty->assign("inbox_number", $inbox_number);
$smarty->assign("inbox_person", $contact_name);
$smarty->assign("inbox_type", $inbox_type);
$smarty->assign("sent_status", $sent_status);
$smarty->assign("start_from", $start_from);
$smarty->assign("gmessages_page", $gmessages_page);
$smarty->assign("number_of_pages", $number_of_pages);
$smarty->assign("number_of_records", $number_of_records);
$smarty->assign("sent_sequence_position", $sent_sequence_position);
$smarty->assign("page", $page);
//for $_GET after sending SMS
$smarty->assign("contact_number_enc", $_GET['contact_number']);
$smarty->assign("contact_name_enc", $_GET['contact_name']);
$smarty->display('templates/conversation_body.tpl');
}
}
else
{
$smarty->display('templates/login_body.tpl');
}
I am using a for loop on my client's website to insert purchased ticket information into a database. The loop is working correctly, but the client has requested the option to attach a unique identifier to the first entry every time the for loop is run. This identifier would be used to highlight the primary ticket owner when the tickets are printed. I have included the current for loop below for reference. Any ideas on how to achieve this? Thank you.
$threepack = '';
$i = '';
for($i = 0; $i < $tickets; $i++)
{
$firstname = 'firstname'.$threepack;
$lastname = 'lastname'.$threepack;
$address = 'address'.$threepack;
$city = 'city'.$threepack;
$postal = 'postal'.$threepack;
$phone = 'phone'.$threepack;
$altphone = 'altphone'.$threepack;
$sec_firstname = 'sec_firstname'.$threepack;
$sec_lastname = 'sec_lastname'.$threepack;
$email = 'email'.$threepack;
$table->firstname = $data->$firstname;
$table->lastname = $data->$lastname;
$table->address = $data->$address;
$table->city = $data->$city;
$table->postal = $data->$postal;
$table->phone = $data->$phone;
$table->altphone = $data->$altphone;
$table->sec_firstname = $data->$sec_firstname;
$table->sec_lastname = $data->$sec_lastname;
$table->email = $data->$email;
$table->id = 0;
$table->order_total = $data->total;
$table->store();
if($data->tickets == '-1')
{
if($threepack == 2)
{
$threepack = 3;
} else {
$threepack = 2;
}
}
// 8 Fields
if($data->tickets == '5')
{
if ($threepack == '') {
$threepack = 2;
} else {
$threepack += 1;
}
}
}
for ($i = 0; $i < $tickets; $i++) {
// ...
if ($i == 0)
$table->highlightme = 1;
// ...
$table->store();
// ...
}
Ok what i'm wanting to do is split a number ($row['count']) into 5, this is easy enough if you want equal numbers:
$sum = ($row['count'] / 5);
$fsum = floor($sum);
but I want each number to be different and still add up to total ie $row['count'] how can this be achieved?
Update:
If this helps its to be used to update 5 rows in a database:
$query = "SELECT * FROM foo";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
$sum = ($row['count'] / 5);
$fsum = floor($sum);
$id = $row['id'];
$update = "UPDATE foo SET foo1='$fsum', foo2='$fsum', foo3='$fsum', foo4='$fsum', foo5='$fsum' WHERE id='$id'";
mysql_query($update);
}// while
so ideally the $update query would be something like:
$update = "UPDATE foo SET foo1='$fsum1', foo2='$fsum2', foo3='$fsum3', foo4='$fsum4', foo5='$fsum5' WHERE id='$id'";
This is my take:
function randomize($sum, $parts) {
$part_no = count($parts);
$continnue_counter = 0;
while (count(array_unique($parts)) != $part_no) {
$changing = array_rand($parts, 2);
if (($parts[$changing[0]] - 1) == 0 || ($parts[$changing[1]] - 1) == 0) { // don't let them go under 1
++$continnue_counter;
// sometime one element get everything and others even out on 1
// just throw away everything you got so far and start over
if ($continnue_counter > 10) {
$parts = setup($sum, $part_no);
$continnue_counter = 0;
}
continue;
}
$continnue_counter = 0;
$signum = mt_rand(0, 100) % 2 ? 1 : -1;
$delta = $signum * mt_rand(1, min($parts[$changing[0]] - 1, $parts[$changing[1]] - 1)); // -1 to make sure they don't go under 0
$parts[$changing[0]] += $delta;
$parts[$changing[1]] -= $delta;
}
return $parts;
}
function setup($sum, $part_no) {
$parts = array_fill(0, $part_no, (int)($sum / $part_no));
// acount for the reminder of (int) cast
$reminder = $sum - array_sum($parts);
while ($reminder) {
$parts[array_rand($parts)] += 1;
--$reminder;
}
return $parts;
}
$part_no = 5;
$sum = 42;
$parts = randomize($sum, setup($sum, $part_no));
var_export($parts);
print array_sum($parts)
Update:
I've added a version that introduces a little more entropy.
Update2:
The more random one had a tendency to decrement everything to 1 except one part, added an explicit detection to deal with this. Still the algorithm behind it has unknown termination time.
This is my webpage:
http://www.autoweek62.uni.cc/carprice.php
Here is the source code for it:
<?php
$dbname = "autoweek_auto1";
$loginname = "autoweek_root";
$loginpass = "PASSWORD (not my real one)";
$dbhost = "localhost";
echo('<html><body bgcolor="#FFFFFF">');
echo('<font face="arial" size="+4"><center>');
echo("Database $dbname");
$id_link = #mysql_connect($dbhost, $loginname, $loginpass);
$tables = mysql_list_tables($dbname, $id_link);
$num_tables = mysql_num_rows($tables);
// store table names in an array
$arr_tablenames[] = '';
// store number of fields per table(index 0,1,2..) in an array
$arr_num_fields[] = '';
for ($i=0; $i < $num_tables; $i++) {
$arr_tablenames[$i] = mysql_tablename($tables, $i);
$arr_num_fields[$i] = mysql_num_fields(mysql_db_query($dbname, "select * from $arr_tablenames[$i]", $id_link));
}
// store field names in a multidimensional array:
// [i] == table number, [ii] == field number for that table
for ($i=0; $i < $num_tables; $i++) {
for ($ii=0; $ii < $arr_num_fields[$i]; $ii++) {
$result = mysql_db_query($dbname, "select * from $arr_tablenames[$i]", $id_link);
$hash_field_names[$i][$ii] = mysql_field_name($result, $ii);
}
}
for ($i=0; $i < $num_tables; $i++) {
echo("<center><h2>Table $arr_tablenames[$i] </h2></center>");
echo('<table align="center" border="1"><tr>');
$result = mysql_db_query($dbname, "select * from $arr_tablenames[$i]", $id_link);
for ($ii=0; $ii < $arr_num_fields[$i]; $ii++) {
echo("<th>");
echo $hash_field_names[$i][$ii];
echo("</th>");
}
echo("</tr><tr>");
$number_of_rows = #mysql_num_rows($result);
for ($iii = 0; $iii < $number_of_rows; $iii++) {
$record = #mysql_fetch_row($result);
for ($ii=0; $ii < $arr_num_fields[$i]; $ii++) {
echo("<td>");
echo $record[$ii];
echo("</td>");
}
echo("</tr>");
}
echo("</table>");
}
echo('</body></html>');
?>
It displays, but I'm not sure how to get it to display in a manner similar to this:
redbook.com.au/new-cars/results.aspx?Ns=p_Make_String|0||p_ClassificationType_String|0||p_Family_String|0||p_Year_String|1||p_SequenceNum_Int32|0&N=2994+2951+4294961316+4294843565&TabId=1407343
[not linked since I can't post more than 1 being new here]
(although mine is simply the make, model, bodystyle and prices as a list, not as complicated as the link above!)
I've Googled for some inspiration, but despite trying haven't got any far. I'm not looking for an instant answer, but any solutions are welcomed!
Displaying images is the trickiest part... I have the JPGs stored on the server, trying to get them to display is a problem.
All help is welcomed!
You could try making a separate script that returns just the image.
In that script you retrieve the image from the database, output mime-type header and then the raw image data. In your main HTML outputting script you just write <img src="getImage.php?id=1234" />