Fatal error: Call to undefined function updateRows() - function

I got this error:
Fatal error: Call to undefined function updateRows()
but I have defined updateRows(), so what is wrong?
Here is my code:
<?php
if ($this->num_rows > 0) {
$i = 0;
while ($row = $stmt->fetchObject()) {
$appleIDS[$i] = array(
$row->id,
$row->pass,
$row->en_b_y,
$row->en_b_m,
$row->en_b_d,
$row->sqa1,
$row->sqa2,
$row->sqa3
);
$i++;
}
updateRows($this->pre_head, $head);
killTheConnection();
return $appleIDS;
}
killTheConnection();
return $check;
}
function updateRows($pre_head, $head) // here is my function
{
$this->mysqli_connet();
try {
$sql = "UPDATE appleid " . "SET taken = 'yes'" . "," . "taken_time=now()" . "WHERE num>=$pre_head AND num<=$head;";
$stmt = $GLOBALS['$connection']->prepare($sql);
$stmt->execute();
echo $stmt->rowCount();
}
catch (PDOException $e) {
echo $sql . "<br>" . $e->getMessage();
}
killTheConnection();
}
?>

Related

Yii::$app->session->setFlash() is not working

Following code in SiteController works fine with echo, but not with yii-method setflash(). Maybe, I have to reconfigure my config-file(main-local.php)?
Any other ideas how to keep setflash() doing its job?
public function actionScript() { //A new method, programmed by Thomas Kipp
$model = new myScriptForm();
$fileName = 'file';
$uploadPath = Yii::getAlias('#uploading');
if (isset($_FILES[$fileName])) {
$file = \yii\web\UploadedFile::getInstanceByName($fileName);
if ($file->saveAs($uploadPath . '/' . $file->name)) {
echo"<script>alert('Hallo');</script>";
//echo \yii\helpers\Json::encode($file);
}
}
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$model->fileImage = UploadedFile::getInstance($model, 'fileImage');
$model->avatar = UploadedFile::getInstances($model, 'avatar');
if ($model->fileImage) {
Yii::$app->session->setFlash('success', 'File has been uploaded');
echo "<font size='4'><br><br><br><center>File <font color='red'> "
. "$model->fileImage<font color='black'> successfully uploaded."
. "<br>It's available in folder 'uploadedfiles' </font></font color></center>";
$model->fileImage->saveAs(Yii::getAlias('#uploadedfilesdir/' . $model->fileImage->baseName . '.' . $model->fileImage->extension));
} else {
Yii::$app->session->setFlash('error', 'There has nothing to be uploaded');
echo"<font size='4'><br><br><br><center>No Upload-file selected.<br>"
. "Nothing moved into folder 'uploadedfiles'</font></center>";
}
if ($model->avatar) {
$counter = 0;
foreach ($model->avatar as $avatar) {
Yii::$app->session->setFlash('success', 'Avatar has been uploaded');
echo "<font size='4'><br><center>File <font color='red'> "
. "$avatar<font color='black'> successfully uploaded."
. "<br>It's available in folder 'uploadedfiles' </font></font color></center>";
$avatar->saveAs(Yii::getAlias('#uploadedfilesdir/' . $avatar->baseName . $counter . '.' . $avatar->extension));
$counter++;
}
} else {
Yii::$app->session->setFlash('error', 'There has nothing to be uploaded');
echo"<font size='4'><br><center>No Upload-file selected.<br>"
. "Nothing moved into folder 'uploadedfiles' </font></center>";
}
return $this->render('myScript', ['model' => $model]);
} else {
return $this->render('myScript_Formular', ['model' => $model]);
}
}
once you have assign the value with eg:
Yii::$app->session->setFlash('error', 'There has nothing to be uploaded');
be sure that in your view eg: myScript.php
you properly echo the value setted
<?= Yii::$app->session->getFlash('error'); ?>

Importing multiple records from json file into DB inside a Laravel project

Solved! Will post the working answer tonight. Might be helpfull to others.
I'm trying to run a script I made a few months back. The script was set up to be used with a directory filled with .json files (45 files containing 200 items each) and it runs through those files to fetch data and then store them with a query in the database.
Problem is I can't use $conn inside my laravel project so I'm at a loss how i can now connect to the database and store all my data. I can read the json no problem and I have a model inside my laravel project for the corresponding table with all fields set to fillable.
Here's my code:
<?php
namespace App\Http\Controllers;
use DB;
use App\Http\Controllers\Controller;
class JsonController extends Controller
{
public function importJson() {
$path = realpath('AllSetFiles/');
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)) as $filename)
{
if ($filename->isDir()) continue;
// Include the JSON and put it inside $data.
$jsondata = file_get_contents($filename);
$data = json_decode($jsondata, true);
// Echo the variables to the screen or put them in the database. (y shows to screen)
$echo = "n";
// foreach card in cards in the JSON file put all values in their named variables.
foreach($data['cards'] as $cards => $values) {
if(isset($values['multiverseid'])) {
$multiverseid = $values['multiverseid'];
} else {
$multiverseid = "";
}
$name = $values['name'];
if (isset($values['manaCost'])) {
$manaCost_array = array();
$manaCost_array = array($values['manaCost']);
$manaCost = implode (", ", $manaCost_array);
} else {
$manaCost = "";
}
if (isset($values['cmc'])) {
$cmc = $values['cmc'];
} else {
$cmc = "";
}
if (isset($values['colors'])) {
$colors_array = array();
$colors_array = $values['colors'];
$colors = implode (", ", $colors_array);
} else {
$colors = "";
}
if (isset($values['type'])) {
$type = $values['type'];
} else {
$type = "";
}
if (isset($values['supertypes'])){
$supertypes_array = array();
$supertypes_array = $values['supertypes'];
$supertypes = implode (", ", $supertypes_array);
} else {
$supertypes = "";
}
if (isset($values['types'])){
$types_array = array();
$types_array = $values['types'];
$types = implode (", ", $types_array);
} else {
$types = "";
}
if (isset($values['subtypes'])){
$subtypes_array = array();
$subtypes_array = $values['subtypes'];
$subtypes = implode (", ", $subtypes_array);
} else {
$subtypes = "";
}
if (isset($values['rarity'])) {
$rarity = $values['rarity'];
} else {
$rarity = "";
}
if (isset($data['code'])) {
$serie = $data['code'];
} else {
$serie = "";
}
if (isset($values['text'])) {
$text = $values['text'];
} else {
$text = "";
}
if (isset($values['flavor'])) {
$flavor = $values['flavor'];
} else {
$flavor = "";
}
if (isset($values['artist'])) {
$artist = $values['artist'];
} else {
$artist = "";
}
if (isset($values['number'])) {
$number = $values['number'];
} else {
$number = "";
}
if (isset($values['power'])) {
$power = $values['power'];
} else {
$power = "";
}
if (isset($values['toughness'])) {
$toughness = $values['toughness'];
} else {
$toughness = "";
}
if (isset($values['layout'])) {
$layout = $values['layout'];
} else {
$layout = "";
}
if (isset($values['imageName'])) {
$imageName = $values['imageName'];
} else {
$imageName = "";
}
if ($echo == "y"){
echo 'multiverseid: <b>' . $multiverseid . '</b></br>';
echo 'name: <b>' . $name . '</b><br/>';
echo 'serie: ' . $serie . '<br/>';
echo 'manaCost:' . $manaCost . '<br/>';
echo 'cmc: ' . $cmc . '<br/>';
echo 'colors: ' . $colors . '<br/>';
echo 'type: ' . $type . '<br/>';
echo 'supertypes:' . $supertypes . '<br/>';
echo 'types: ' . $types . '<br/>';
echo 'subtypes: ' . $subtypes . '<br/>';
echo 'rarity: ' . $rarity . '<br/>';
echo 'text: ' . $text . '<br/>';
echo 'flavor: ' . $flavor . '<br/>';
echo 'artist: ' . $artist . '<br/>';
echo 'number: ' . $number . '<br/>';
echo 'power: ' . $power . '<br/>';
echo 'toughness: ' . $toughness . '<br/>';
echo 'layout: ' . $layout . '<br/>';
echo 'imageName: ' . $imageName . '<br/>';
echo 'src="http://gatherer.wizards.com/Handlers/Image.ashx?multiverseid=' . $multiverseid . '&type=card"<br/>';
echo '<br/><hr><br>';
} else {
// Put all variables in a query and send it to the database.
DB::table('cards')->insert(
[
'multiverseid' => $multiverseid,
'name' => $name,
'serie' => $serie,
'manaCost' => $manaCost,
'cmc' => $cmc,
'colors' => $colors,
'type' => $type,
'supertypes' => $supertypes,
'types' => $types,
'subtypes' => $subtypes,
'rarity' => $rarity,
'text' => $text,
'flavor' => $flavor,
'artist' => $artist,
'number' => $number,
'power' => $power,
'toughness' => $toughness,
'layout' => $layout,
'imageName' => $imageName
]
);
} // end if show or query loop
} // end foreach loop
} // end foreach recursiveIterator
} // end of function importJson
} // end of class
?>
And in my routes.php:
// JSON
Route::get('importjson', 'JsonController#importJson');
going to myapp.com/importjson gives me the following error:
Class 'App\Http\Controllers\RecursiveIteratorIterator' not found

MYSQL Error, what did i do wrong?

If I remove the or die("MySQL ERROR: ".mysqli_error()); from the code below, it gives me no error. But it also doesn't write to the database.
What am I doing wrong?
ob_start();
session_start();
require ('openid.php');
function logoutbutton() {
echo "<form action=\"steamauth/logout.php\" method=\"post\"><input value=\"Logout\" type=\"submit\" /></form>"; //logout button
}
function steamlogin() {
try {
require("steamauth/settings.php");
$openid = new LightOpenID($steamauth['domainname']);
$button['small'] = "small";
$button['large_no'] = "large_noborder";
$button['large'] = "large_border";
$button = $button[$steamauth['buttonstyle']];
if(!$openid->mode) {
if(isset($_GET['login'])) {
$openid->identity = 'http://steamcommunity.com/openid';
header('Location: ' . $openid->authUrl());
}
//echo "<form action=\"?login\" method=\"post\"> <input type=\"image\" src=\"http://cdn.steamcommunity.com/public/images/signinthroughsteam/sits_".$button.".png\"></form>";
} elseif($openid->mode == 'cancel') {
echo 'User has canceled authentication!';
} else {
if($openid->validate()) {
$id = $openid->identity;
$ptn = "/^http:\/\/steamcommunity\.com\/openid\/id\/(7[0-9]{15,25}+)$/";
preg_match($ptn, $id, $matches);
$_SESSION['steamid'] = $matches[1];
include_once("set.php");
$query = mysqli_query("SELECT * FROM users WHERE steamid='".$_SESSION['steamid']."'");
if (mysqli_num_rows($query) == 0) {
mysqli_query("INSERT INTO users (steamid) VALUES ('".$_SESSION['steamid']."')") or die("MySQL ERROR: ".mysqli_error());
}
if (isset($steamauth['loginpage'])) {
header('Location: '.$steamauth['loginpage']);
}
} else {
echo "User is not logged in.\n";
}
}
} catch(ErrorException $e) {
echo $e->getMessage();
}
}
Here's a screen-shot of my database structure:

Registering user with PDO

I have a problem while processing the register
Here is the code for the function:
<?php
include $_SERVER['DOCUMENT_ROOT']. "/config/config.php";
$conn = new PDO('mysql:host=' . $ip . ';dbname=' . $database, $username, $password);
function registerUser($username, $password, $passwordagain, $email, $mcname)
{
$validusername= "/^[a-z0-9]+$/";
$validpassword= "/^[A-Za-z0-9]+$/";
$validemail= "/^([a-zA-Z0-9_\.\-])+\#(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/";
$validmcname= "/^[A-Za-z0-9]+$/";
$error = 0;
if(strlen($username)<4 || strlen($username)>24)
{
$error = 1;
}
if(strlen($password)<6 || strlen($password)>24)
{
$error = 1;
}
if(strlen($mcname)<4 || strlen($mcname)>24)
{
$error = 1;
}
if(!preg_match($validusername, $username))
{
$error = 1;
}
if(!preg_match($validpassword, $password))
{
$error = 1;
}
if(!preg_match($validemail, $email))
{
$error = 1;
}
if(!preg_match($validmcname, $mcname))
{
$error = 1;
}
if($password != $passwordagain)
{
$error = 1;
}
$userexistquery = $conn->query('SELECT * FROM users WHERE username=' . $username);
$userexist = $userexistquery->fetch();
if(count($userexist) != 0)
{
$error=1;
echo "<div class='erroralert'>Username already exists!</div>";
}
$emailexistquery = $conn->query('SELECT * FROM users WHERE email=' . $email);
$emailexist = $emailexistquery->fetch();
if($emailexist != 0)
{
$error=1;
echo "<div class='erroralert'>E-mail already exists!</div>";
}
if($error != 1)
{
$encryptedpassword = hash('sha512', $password);
$registeruser = $conn->query("INSERT INTO users(username, password, email, mcname) VALUES ('$username', '$encryptedpassword', '$email', '$mcname')");
echo "<div class='successalert'>Succesfully registred</div>";
}
else
{
echo "<div class='erroralert'>An error occured during registration.</div>";
}
}
?>
The error that gives me is:
Notice: Undefined variable: conn in C:\xampp\htdocs\functions\functions.php on line 45
Fatal error: Call to a member function query() on a non-object in C:\xampp\htdocs\functions\functions.php on line 45
You instantiate your variable $conn outside of the function. You have to pass it as an argument in order to use it inside.

Live Search With Mysql and PHP

I am building a Live Search function on my website, I used the W3schools example, which worked perfectly. But I want to use MySQL database instead of the XML file, so I am working on a code to take the MySQL database and turn it into an XML file.
<?php
header("Content-type: text/xml");
include 'dbc.php';
$query = "SELECT * FROM airports";
$result = mysql_query($query, $link)
or die('Error querying database.');
$xml_output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
$xml_output .= "<entries>\n";
for($x = 0 ; $x < mysql_num_rows($result) ; $x++){
$row = mysql_fetch_assoc($result);
$xml_output .= "\t<entry>\n";
$xml_output .= "\t\t<ident>" . $row['ident'] . "</ident>\n";
// Escaping illegal characters
$row['name'] = str_replace("&", "&", $row['name']);
$row['name'] = str_replace("<", "<", $row['name']);
$row['name'] = str_replace(">", ">", $row['name']);
$row['name'] = str_replace("\"", """, $row['name']);
$xml_output .= "\t\t<name>" . $row['name'] . "</name>\n";
$xml_output .= "\t</entry>\n";
}
$xml_output .= "</entries>";
echo $xml_output;
?>
I am getting this error:
Warning: DOMDocument::load() [domdocument.load]: Start tag expected, '<' not found in /public_html/sql2xml.php, line: 11 in /public_html/livesearch.php on line 12
no suggestion
I have read the explanation at: Avoid DOMDocument XML warnings in php
But I have no idea how to fix that in my code. Any suggestions?
This will take the data from the 'Name' column in your mysql table and put it into an array its the same array w3c school looks up as you type
$mysqli = new mysqli(HOST,USER,PASSWORD,DATABASE);
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$query = "SELECT * FROM table";
$result = $mysqli->query($query);
while($row = $result->fetch_array())
{
$a[]=$row["Name"];
}
/* free result set */
$result->free();
/* close connection */
$mysqli->close();
// get the q parameter from URL
$q = $_REQUEST["q"];
$hint = "";
// lookup all hints from array if $q is different from ""
if ($q !== "") {
$q = strtolower($q);
$len=strlen($q);
foreach($a as $name) {
if (stristr($q, substr($name, 0, $len))) {
if ($hint === "") {
$hint = $name;
} else {
$hint .= ", $name";
}
}
}
}
if (isset($q))
{
// Output "no suggestion" if no hint was found or output correct values
echo $hint === "" ? "no suggestion" : $hint;
}