php page doesn't rendering when connect to database - php

I'm making simple php web page, using php5, mysql, Nginx.
when connect to database, it suddenly freeze, and render nothing to browser..
my php code is this.
<?php
$host = 'localhost';
$user = 'root';
$password = '123456';
$db_name = 'test';
$link = new mysqli($host, $user, $password, $db_name);
if($link->connect_errno) {
echo "<script>alert('Connection Error....')</script>";
}
if($link) {
echo "<script>alert('Successfully connected!');</script>";
}
?>
(lib/connect_db.php)
<?php
include './lib/connect_db.php';
$create_user_query = 'CREATE TABLE ...';
$admin_user_query = 'INSERT INTO ....';
$create_widget_query = 'CREATE TABLE ...';
if($link->query($create_user_query) === TRUE) {
echo "<script>alert('Success!')</script>";
}
$link->close();
?>
(init.php)
index.php also include './lib/connect_db.php', it is OK. but init.php always causing problem.
p.s. when I edited init.php to
<script>alert('...');</script>
<?php
...
?>
it doesn't call alert();

you have syntax error you missing $ before variable in (lib/connect_db.php)
include '.lib/connect_db.php';
$create_user_query = 'CREATE TABLE ...';
$admin_user_query = 'INSERT INTO ....';
$create_widget_query = 'CREATE TABLE ...';
if($link->query($create_user_query) === TRUE) {
echo "<script>alert('Success!')</script>";
}
$link->close();

Related

form values will not save into PhpMyAdmin or MYSQL workbench is something wrong with my connection to the database

this is the code i am using to send data to phpmyadmin from my squidgameeditor3.php and insert2.php pages.
squidgameeditor3.php
<form action="insert2.php" method="POST" target="votar">
insert3.php
<?php
//Database Credentials
$host = 'localhost:3306';
$database = 'circulartexter';
$username = 'root';
$password = '';
try {
$DBH = new PDO("mysql:host=$host;dbname=$database", $username, $password); }
catch(PDOException $e) {
echo "did catch that! ";
echo $e->getMessage(); }
echo " #it works";
$sz1 = $_POST['sz1'];
$tp1 = $_POST['tp1'];
$lh1 = $_POST['lh1'];
$ft1 = $_POST['ft1'];
$sp1 = $_POST['sp1'];
$sh1 = $_POST['sh1'];
$sk1 = $_POST['sk1'];
$ma1 = $_POST['ma1'];
$sql = "INSERT INTO atrmain (`sz1`,`tp1`,`lh1`,`ft1`,`sp1`,`sh1`,`sk1`,`ma1`) SELECT ('$sz1','$tp1','$lh1','$ft1','$sp1','$sh1','$sk1','$ma1')";
?>
phpmyadmin
i looked everywhere cant find the answer but i know its simple because i had this code working on another laptop before but it wont work on my new laptop so as soon as i get this question answered i will delete it

PHP MySQL DELETE row from html table with link button

I've been looking at this for some time now, all I'm doing is learning PHP with mysql, with no previous programming experience before these so please be kind. I need to delete an entire row from a table. The table is inside list.php and there's a delete button that redirects to delete.php. But all it does it redirects me to a blank page with an url something like "delete.php?id=4" depending on row ID, which seems to be correct.
This is connect.php:
<?php
$server = "localhost";
$db_user = "root";
$db_pass = "";
$db_name = "school";
$db_conn = mysqli_connect($server, $db_user, $db_pass, $db_name);
$connect_error = 'Sorry, we are experiencing connection problems';
mysql_connect('localhost', 'root', '') or die($connect_error);
mysql_select_db('school') or die($connect_error);
?>
And this is delete.php :
<?php
if(isset($_GET['ID'])){
$courseID = $_GET['ID'];
$sql_delete = "DELETE FROM Courses WHERE ID = $courseID";
print ($sql_delete);
$result = mysqli_query($db_conn,$sql_delete);
if($result) {
echo "Congratulations. You have deleted this course succesfully.";
header('location:list.php');
} else {
echo "Error";
error_reporting(E_ALL ^ E_DEPRECATED);
}
}
?>
Notice "print ($sql_delete);", which doesn't print anything either. All I can think of is there is something wrong with the way I am asking for the ID by using $_GET, but can't get my head around it. When I run
"DELETE FROM Courses WHERE ID = 4;"
inside xampp's mysql module, it works. Uhm... oh well.
OK, let me add:
This is actually my delete button inside list.php, which gives me an URL based on table row number, based on Course id.
<?php echo "<td>Delete";?>
connect.php
$server = "localhost";
$db_user = "root";
$db_pass = "";
$db_name = "school";
$db_conn = mysqli_connect($server, $db_user, $db_pass, $db_name);
delete.php
if(isset($_GET['ID'])){
$courseID = $_GET['ID'];
$sql_delete = "DELETE FROM Courses WHERE ID = $courseID";
$result = mysqli_query($db_conn,$sql_delete);
if(mysqli_affected_rows($db_conn)>0) {
header('location:list.php?result=success');
} else {
header('location:list.php?result=fail');
}
}
list.php (Where is delete button)
<?php
if(isset($_GET['result'))
{
if($_GET['result']=='success')
{
echo "Congratulations. You have deleted this course succesfully.";
}
else{
echo "error";
}
}
?>
(Delete button)
<a href="delete.php?ID=5" >Delete</a>

Requesting assistance regarding basic php PDO connection

(Disclaimer:
This code is from Chapter 11 of the book "PHP Solutions: Dynamic Web Design Made Easy" Copyright David Powers, 2006. It is a great resource in my opinion, speaking from the perspective of an obviously very novice web developer. Here is the source code for the entire book. Some of this code is a bit different from what is in the book itself. )
** edit: corrected code according to first comment by Benny Hill.
** edit for Solution: case closed, with the help of Benny Hill.
Solution: After correcting my code, I had to manually direct the browser to the file: http:// localhost/siteG/mysql/pdo.php (<-- this is correct), versus dragging the file straight into the browser, producing this URL: file:///C:/wamp/www/siteG/mysql/pdo.php This is what was messing me up. Thank you Benny Hill.
Hello,
I am having trouble connecting to my DB with the following code.
Here is what my browser outputs:
query($sql); $error = $conn->errorInfo(); if (isset($error[2])) die($error[2]); // find out how many records were retrieved $numRows = $result->fetchColumn(); echo $numRows; ?>
Here is the code :
Connection File: conn_pdo.inc.php
<?php
// call this function like this: $conn=dbConnect('admin') or, admin can be query
function dbConnect($type) {
if ($type == 'query') {
$user = 'root';
$pwd = 'root';
}
elseif ($type == 'admin') {
$user = 'root';
$pwd = 'root';
} else {
exit('Unrecognized connection type');
}
}
function dbConnect() {
try {
$conn = new PDO('mysql:host=localhost;dbname=basicgallery', $user, $pwd);
return $conn;
}
catch (PDOException $e) {
echo 'Cannot connect to database';
exit;
}
} ?>
the "query" file: pdo.php
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<?php
include('../includes/conn_pdo.inc.php');
// connect to MySQL
$conn = dbConnect('query');
// Prepare sql query
$sql = 'SELECT COUNT(*) FROM images';
// submit the query and capture the result
$result = $conn->query($sql);
$error = $conn->errorInfo();
if (isset($error[2])) die($error[2]);
// find out how many records were retrieved
$numRows = $result->fetchColumn();
echo $numRows;
?>
</body>
</html>
Can anyone help me figure out why the browser is outputting raw php code?
Your conn_pdo.inc.php file should look like this:
<?php
// call this function like this: $conn=dbConnect('admin') or, admin can be query
function dbConnect($type){
if ($type == 'query'){
$user = 'root';
$pwd = 'root';
}
elseif ($type == 'admin'){
$user = 'root';
$pwd = 'root';
}else{
exit('Unrecognized connection type');
}
try{
$conn = new PDO('mysql:host=localhost;dbname=basicgallery', $user, $pwd);
return $conn;
}
catch (PDOException $e){
echo 'Cannot connect to database';
exit;
}
}
?>
I removed the second declaration of dbConnect and the closing brace for the first declaration of dbConnect.

php mysql script not working

I got my script working for one column of data but I am trying to send it other data to a second column in mysql table. Here's my php code:
<?php
function db_connect()
{
$hostname = '127.0.0.1';
$db_user = 'root';
$db_password = '';
$db_name = 'hit';
mysql_connect ($hostname, $db_user, $db_password) or die (mysql_error());
echo "Success.. Connected to MySQL...<br />";
mysql_select_db($db_name) or die(mysql_error());
echo "Success.. Connected to Database...<br /> ";
}
function insertData($DATA)
{
function insterData($DATA2)
{
db_connect();
$requete = "INSERT INTO data SET col_Data='".$DATA."'";
if(!mysql_query($requete))
echo mysql_error();
else
echo 'data accepted.';
$requete2 = "INSERT INTO data SET col_Data2='".$DATA2."'";
if(!mysql_query($requete2))
echo mysql_error();
else
echo 'data accepted.';
}
if(isset($_GET['DATA']))
if(isset($_GET['DATA2']))
}
insertData($_GET['DATA']);
insertData($_GET['DATA2']);
}
else
{
echo 'Nop';
}
?>
This is how I send the post data
http://localhost/hit.php?DATA=iamwicked&DATA2=iamcool
This then suppose to send DATA=iamwicked goes into database hit table data column col_data
This then suppose to send DATA2=iamcool goes into database hit table data column col_data2
But I get this error,
but there are errors can someone help me debug.
Here is a working script:
<?php
function db_connect()
{
$hostname = '127.0.0.1';
$db_user = 'root';
$db_password = '';
$db_name = 'hit';
mysql_connect ($hostname, $db_user, $db_password) or die (mysql_error());
echo "Success.. Connected to MySQL...<br />";
mysql_select_db($db_name) or die(mysql_error());
echo "Success.. Connected to Database...<br /> ";
}
function insertData($DATA)
{
db_connect();
$requete = "INSERT INTO data SET col_Data='".$DATA."'";
if(!mysql_query($requete))
echo mysql_error();
else
echo 'data accepted.';
}
if(isset($_GET['DATA']))
{
insertData($_GET['DATA']);
}
else
{
echo 'Nop';
}
?>
this is a working script when I use this url to post data
localhost/hit.php?DATA=iamwicked
When I use this it save iamwicked in database hit table data column col_data
so how do I fix my script to send more data to col_data2 and so forth
Return $conn connection resource #id from function
<?php
function db_connect()
{
$hostname = '127.0.0.1';
$db_user = 'root';
$db_password = '';
$db_name = 'hit';
$conn = mysql_connect ($hostname, $db_user, $db_password) or
die (mysql_error());
echo "Success.. Connected to MySQL...<br />";
mysql_select_db($db_name) or die(mysql_error());
echo "Success.. Connected to Database...<br /> ";
return $conn;
}
$conn = db_connect();
To insert single field
function insertData($DATA)
{
$requete = "INSERT INTO data SET col_Data='".$DATA."'";
mysql_query($requete) or die(mysql_error());
}
if(isset($_GET['DATA'])) {
insertData($_GET['DATA']);
}
if(isset($_GET['DATA2'])) {
insertData($_GET['DATA2']);
}
UPDATE
To insert multiple fields
function insertData($DATA, $DATA2)
{
$requete = "INSERT INTO data SET col_Data='".$DATA."', col_Data2='".$DATA2."'";
mysql_query($requete) or die(mysql_error());
}
if(isset($_GET['DATA']) && isset($_GET['DATA2'])) {
insertData($_GET['DATA'], $_GET['DATA2']);
}
?>
I think you have an wrong spelling here:
function insertData($DATA2) instead of function insterData($DATA2);
There are indeed two problems here.
function insertData($DATA)
{
function insterData($DATA2)
{
What are you trying to achieve here? Declaring a function inside another function is totally useless (and generates errors since it's not allowed). If you want to call a function inside another one you must declare them separately and then call them, f.e.
function insertData($DATA)
{
insterData($somevariable);
//Rest of the operations
}
This should be clear enough. There is another error though.
if(isset($_GET['DATA']))
if(isset($_GET['DATA2']))
}
insertData($_GET['DATA']);
insertData($_GET['DATA2']);
}
else
{
echo 'Nop';
}
I suppose there is a typo here, and you meant
if(isset($_GET['DATA2']))
{

XML request MySQL by PHP

Actually i'm working in a flash web page using XML. I want to make a sing up page with XML that uses PHP to insert data into de MySQL database, but I'm stuck... and i my XML-PHP knowledge are not enough for this duty.
This is the XML file:
<?xml version="1.0" encoding="utf-8"?>
<data>
<title>Sing up</title>
<request field1="user" field2="email" field3="password" field4="other">reg.php</request>
<description><![CDATA[Please sing up!]]></description>
</data>
The reg PHP file:
<?php
function Reg()
{
if (isset($_POST['reg'])==true) {
require_once('db_conf.php');
$user = $_POST['username'];
$pass = sha1(strtoupper($user.':'.$_POST['password']));
$email = $_POST['email'];
$con = mysql_connect($dbhost, $dbuser, $dbpassword);
if (!$con)
{
die('Could not connect!');
} else {
mysql_select_db("$logondb", $con);
$sql="INSERT INTO accounts (username, sha_pass_hash, email) VALUES ('$user','$pass','$email')";
if (!mysql_query($sql,$con))
{
die('Error creating account.');
}
echo $succesmsg;
mysql_close($con);
}
} else {
?>
And PHP conf for database:
<?php
$dbhost = 'localhost';
$dbuser = 'user';
$dbpassword = 'password';
// Accounts Database
$logondb = 'accounts';
$errormsg="Error creating account..";
$succesmsg="Account created!";
?>
I would like to suggest using PDO instead - it's a more general approach. And then you should use data-binding to reduce vulnerability against SQL Injections...
This should take you one step further:
<?php
function Reg()
{
if (isset($_POST['reg'])==true) {
require_once('./db_conf.php');
$user = $_POST['username'];
$pass = sha1(strtoupper($user.':'.$_POST['password']));
$email = $_POST['email'];
$dsn = "mysql:host=$dbhost;dbname=$logondb";
$pdo = new PDO($dsn,$dbuser,$dbpassword);
if (!$pdo)
{
die('Could not connect!');
} else {
$sql = "INSERT INTO accounts (username, sha_pass_hash,email) VALUES (:user,:pass,:email)";
$stmt = $pdo->prepare($sql);
$res = $stmt->execute(array("user"=>$user , "pass"=>$pass , "email" => $email));
if (!$res) {
echo "Error :((<pre>";
var_dump($stmt->errorInfo());
echo "</pre>";
} else
{
echo $succesmsg;
}
}
} else {
echo "reg was not set - terminating...!";
}
}
Reg(); // execute it!
?>

Categories