could use some help with this script im making to help learn

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ortsa
Forum Newbie
Posts: 3
Joined: Thu Apr 12, 2007 2:33 pm

could use some help with this script im making to help learn

Post by ortsa »

Jcart | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


learning php decided id make a little database editing program to help me 
getting a error and i cant find whats wrong here is my code

Code: Select all

<?php

?>
<html>
<head>
<link href="../../style.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div align="center" class="style7">
  <p class="style5"><h1>Database Editing Program</h1></p>
</div>
<?php echo "<h3>Insert Data</h3><form method='POST' action='proccess.php'>
	<input type='hidden' name='type' value='insert'>
    Columns To insert into (<I>separate with commas)</i>
	<input name='selection' type='text'><br>
	Table Name
    <input type='text' name='insert_table_name'><br>
	Data to insert 
    <input name='insert_inserts' type='text'> 
    <input type='submit' name='Submit' value='Submit'>
    </form>";
?>
<?php
echo "<h3>Truncate Table</h3>
<form action='proccess.php' method='POST'>
Truncate <input type='text' name='truncate_table_name'/>
<input type='submit' name='Submit' value='Sumbit' />
<input type='hidden' name='type' value='truncate'>
</form>
"
?>
</body>
</html>
Thats the interface page and heres the proccesing page

Code: Select all

<link href="../../style.css" rel="stylesheet" type="text/css">
<?php
include ("../../includes/connect.inc.php");
@$type = $_POST['type'];
@$insert_selection = $_POST['selection'];
@$insert_table_name = $_POST['insert_table_name'];
@$insert_inserts = $_POST['insert_inserts'];

@$truncate_table = $_POST['truncate_table_name'];
@$truncate_succesfull;
?>

<?php
// Function to insert data
function insert($insert_selection,$insert_table_name,$insert_inserts, $conn, $db) {
    $sql = "INSERT INTO $insert_table_name ($insert_selection) VALUES ('$insert_table_name')";
    $result = mysqli_query($conn, $sql);
}
// function to truncate
function truncate($truncate_table, $conn, $db) {
	$sql = "TRUNCATE $truncate_table";
	$result = mysqli_query($conn, $sql);
	$truncate_succesfull = "1";
	}
?>

<?php

if ($type == "insert")
{
    insert($type, $insert_selection, $insert_table_name, $insert_inserts, $conn, $db) or die("Error - Cannot Insert Data!");
    
}
elseif ($type == "truncate") {
	truncate($truncate_table, $conn, $db);
	if ($truncate_succesfull == "1") {
		echo "Truncate Succesfull!";
	}
	
}
elseif (!isset($type)) {
	echo "<h1>Do Not Run this Page On Its Own You Wont Get Any Result</h1>";
}
?>
and when i insert something i get this error truncate works fine

Code: Select all

Warning: mysqli_query() expects parameter 1 to be mysqli, string given in C:\Documents and Settings\Peter\Desktop\www\apache\htdocs\cms\admin\db\proccess.php on line 17
Also any surgestions for improving my code would be great
Thank you all


Jcart | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Last edited by ortsa on Sun Apr 15, 2007 6:46 am, edited 1 time in total.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Where is $conn being set?

In the future, it helps to highlight your posted code.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Show the part of the include that connects to the database (without the real credentials is fine). Also, get rid of all the error suppression. That will only hurt you in the long run.
ortsa
Forum Newbie
Posts: 3
Joined: Thu Apr 12, 2007 2:33 pm

Post by ortsa »

here is the page connect.inc.php

Code: Select all

<?php

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'my_password_hands_off';
$dbname = 'main';

$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
$db = mysqli_select_db($conn,$dbname);
?>
btw what do you think of my code?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You have no error checking on your connection. Try adding some of that in. Here's an example from the manual.

Code: Select all

<?php
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);

/* check connection */
if (!$conn) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
?>
ortsa
Forum Newbie
Posts: 3
Joined: Thu Apr 12, 2007 2:33 pm

Post by ortsa »

i put that code in and get no errors
i put the '@' in front of the variables because otherwise i get loads of notices about indefined variables
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

That means that you are trying to use variables that are not defined. It is good to have those notices so you know where you need to fix your code.
Post Reply