Php and Msql (help solve a coding error somewhere...)

Started on the old forum in May 2014 · view the original in the archive · old links redirect here

3 replies · 111 views · started 12 years ago · 1 min read

4 participants
cobusbo Developer · 117 posts #1

Hi ive tried to modify this script to add a new field named Title into the table via the install.php

<?php
include "admin/connect.php";
$createquotes="CREATE TABLE rquote_quotes (
  ID bigint(21) NOT NULL auto_increment,
  Title mediumtext NOT NULL,
  quote mediumtext NOT NULL,
  PRIMARY KEY  (ID)
)";
mysql_query($createquotes) or die("Could not create quotes");
$createadmins="CREATE TABLE rquote_login (
  ID int(10) NOT NULL auto_increment,
  username varchar(255) NOT NULL default '',
  password varchar(255) NOT NULL default '',
  PRIMARY KEY  (ID)
)";
mysql_query($createadmins) or die("Could not create admins");

print "Installed";
?>

I think here aint a problem in the above modification...

But then I modified then I started modifing the Addqoute.php file

<?
include "connect.php";
session_start();
if (isset($_SESSION['username']))
 {
      print "<A href='addquote.php'>Add a quote</a> | <A href='deletequote.php'>Delete a quote</a>|<A href='search.php'>Search</a><br><br>";
     if(isset($add))
      {
        $addquote="Insert into rquote_quotes (quote) values ('$Title','$quote')";
        $r=mysql_query($addquote) or die("Could not insert quote");
        if($r)
           {
             print "Quote added successfully";
           }
       }
     else
      {
        print "<form action='addquote.php' method='post'>";
        print "Type quote here<br>";
        print "<textarea name='Title' rows='3' cols='20'></textarea><br>";
print "<textarea name='quote' rows='3' cols='20'></textarea><br>";

        print "<input type='submit' name='add' value='add quote'></form>";
      }
   
     
 }

else   
  {
    print "Not logged in as Administrator, please <A href='login.php'>Login</a>";
  }

?>

by adding the lines

$addquote="Insert into rquote_quotes (quote) values ('$Title','$quote')";

and also

print "<textarea name='Title' rows='3' cols='20'></textarea><br>";

im not sure if this modifications is done correctly.

then in the deletequote.php file

<?
include "connect.php";
session_start();
if (isset($_SESSION['username']))
 {
      print "<A href='addquote.php'>Add a quote</a> | <A href='deletequote.php'>Delete a quote</a>|<A href='search.php'>Search</a><br><br>";
     if(isset($delete))
      {
        $ID=$_GET[ID];
        $deletequote="Delete from rquote_quotes where ID='$ID'";
        $r=mysql_query($deletequote) or die("Could not delete quote");
        if($r)
           {
             print "Quote delete successfully";
           }
       }
     else
      { 
        $quotedelete="SELECT * from rquote_quotes";
        $quotedelete2=mysql_query($quotedelete) or die("Could not select");
        while($r3=mysql_fetch_array($quotedelete2))
         {
           print "$r3[Title]<br>";
        print "$r3[quote]<br>";
           print "<form action='deletequote.php?ID=$r3[ID]' method='post'>";
           print "<input type='hidden' name='ID' value='$r3[ID]'>";
           print "<input type='submit' name='delete' value='delete quote'><br>";   
           print "</form>";        
        }
   
     }
}

else   
  {
    print "Not logged in as Administrator, please <A href='login.php'>Login</a>";
  }

?>

i added the lines

print "$r3[Title]<br>";

on the index.php

<?
include "admin/connect.php";
$randomize="SELECT*FROM rquote_quotes";
$randomize2=mysql_query($randomize);
$numrow=mysql_num_rows($randomize2);

$j=RAND()%$numrow+1;

$r="SELECT * FROM rquote_quotes where ID='$j' LIMIT 1";
$r2=mysql_query($r);
while($r3=mysql_fetch_array($r2))
{
print "$r3[Title]<br>";
print "$r3[quote]";
}
?>

I added the line

print "$r3[Title]<br>";

I'm not sure where I did what wrong can you help me solve my problem please?

subzero Super moderator Fellow · 3,954 posts #2

('$Title','$quote')"; if you're mysql starts $quote, $title ?

Visit: Chat4u.mobi - The New Lay Of being a site of your dreams! Visit: WapMasterz Coming Back Soon!


SCRIPTS FOR SALE BY SUBZERO Chat4u Script : coding-talk.com/f28/chat4u-mobi-script-only-150-a-17677/ - > Best Script for your site no other can be hacked by sql or uploaders. FileShare Script : coding-talk.com/f28/file-wap-share-6596/ -> Uploader you will never regret buying yeah it mite be old now but it still seems to own others... _______…

something else Fellow · 2,296 posts #3

subzero wrote: ('$Title','$quote')"; if you're mysql starts $quote, $title ?

$addquote="Insert into rquote_quotes (Title, quote) values (mysql_real_escape_string($_POST['Title']),mysql_real_escape_string($_POST['quote']))";

/////////////////////////// if(isset($add)) should be: if(isset($_POST['add'))

/////////////////////////// $ID=$_GET[ID]; should be $ID=$_GET['ID']; to avoid errors, better still: $ID = mysql_real_escape_string($_GET['ID']);

/////////////////////////// print "$r3 "; should be on the lines of: print $r3['quote']." ";

/////////////////////////// $randomize="SELECT*FROM rquote_quotes"; $randomize2=mysql_query($randomize); $numrow=mysql_num_rows($randomize2); $j=RAND()%$numrow+1; $r="SELECT * FROM rquote_quotes where ID='$j' LIMIT 1";

can be less calls to database:

$r="SELECT * FROM rquote_quotes where ORDER BY RAND LIMIT 1";

/////////////////////////// print "<textarea name='Title' rows='3' cols='20'></textarea> "; maybe better as: print '<input type="text" name="Title"/> ';

👍 1
Share
xor Junior · 13 posts #4

I have optimize,modify & rewrite

install.php

<?php
include 'admin/connect.php';

$tables = array(
    'quotes' => 'CREATE TABLE rquote_quotes (ID bigint(21) NOT NULL auto_increment,Title mediumtext NOT NULL,quote mediumtext NOT NULL,PRIMARY KEY  (ID))',
    'admins' => 'CREATE TABLE rquote_login (ID int(10) NOT NULL auto_increment,username varchar(255) NOT NULL default \'\',password varchar(255) NOT NULL default \'\',PRIMARY KEY  (ID))'
);

foreach($tables as $table=>$query) {
    if( mysql_query($query) {
        echo '<pre>'.$table.'succesfully created</pre>';
    } else {
        echo '<pre>'. mysql_error() .'</pre>';
    }
}
?>

addquote.php

<?
include 'connect.php';

session_start();

if (isset($_SESSION['username'])) {
    
    print '<a href="addquote.php">Add a quote</a> | <a href="deletequote.php">Delete a quote</a>|<a href="search.php">Search</a><br><br>';

    if (isset($_POST['title'], $_POST['quote'])) {
        $title = mysql_real_escape_string($_POST['title']);
        $quote = mysql_real_escape_string($_POST['quote']);
        
        if (mysql_query("INSERT INTO rquote_quotes(`title`,`quote`) values ('{$title}','{$quote}')")) {
            echo 'Quote added successfully"';
        } else {
            echo '<pre>'. mysql_error() .'</pre>';
        }
    } else {
        echo '<form action="addquote.php" method="post">';
        echo '<div>Type quote here</div>';
        echo '<div><textarea name="title" rows="3" cols="20"></textarea></div>';
        echo '<div><textarea name="quote" rows="3" cols="20"></textarea></div>';
        echo '<input type="submit" name="add" value="add quote">';
        echo '</form>';
    } else{
        echo '<p>Not logged in as Administrator, please <a href="login.php">Login</a></p>';
    }
?>

deletequote.php

<?
include "connect.php";

session_start();

if (isset($_SESSION['username'])) {
    print "<p><A href='addquote.php'>Add a quote</a> | <A href='deletequote.php'>Delete a quote</a>|<A href='search.php'>Search</a></p>";

    if(isset($_GET['ID'], $_GET['delete'])) {
        $ID = (int) $_GET[ID];
        
        if ( mysql_query("DELETE FROM rquote_quotes where ID={$ID}") {
            print "Quote delete successfully";
        } else {
            echo '<p>Error: '. mysql_error() .'</p>';
        }
    } else {
        $query = mysql_query(SELECT * from rquote_quotes) or die('Error:' .mysql_error());
            
        while($row = mysql_fetch_assoc($query)) {
            echo '<h3>'. $r3[Title] .'</h3>';
            echo '<div>'. $r3[quote] .'</div>';
            
            echo "<form action='deletequote.php?ID=$r3[ID]' method='post'>";
            echo "<input type='hidden' name='ID' value='$r3[ID]'>";
            echo "<input type='submit' name='delete' value='delete quote'><br>";
            echo "</form>";
        }
    }
} else {
    print "Not logged in as Administrator, please <A href='login.php'>Login</a>";
}
?>

index.php

<?
include "connect.php";

session_start();

if (isset($_SESSION['username'])) {
    print "<p><A href='addquote.php'>Add a quote</a> | <A href='deletequote.php'>Delete a quote</a>|<A href='search.php'>Search</a></p>";

    if(isset($_GET['ID'], $_GET['delete'])) {
        $ID = (int) $_GET[ID];
        
        if ( mysql_query("DELETE FROM rquote_quotes where ID={$ID}") {
            print "Quote delete successfully";
        } else {
            echo '<p>Error: '. mysql_error() .'</p>';
        }
    } else {
        $query = mysql_query(SELECT * from rquote_quotes) or die('Error:' .mysql_error());
            
        while($row = mysql_fetch_assoc($query)) {
            echo '<h3>'. $r3[Title] .'</h3>';
            echo '<div>'. $r3[quote] .'</div>';
            
            echo "<form action='deletequote.php?ID=$r3[ID]' method='post'>";
            echo "<input type='hidden' name='ID' value='$r3[ID]'>";
            echo "<input type='submit' name='delete' value='delete quote'><br>";
            echo "</form>";
        }
    }
} else {
    print "Not logged in as Administrator, please <A href='login.php'>Login</a>";
}
?>
👍 1
Share

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close