Jump to content

php/sql problem


Imaulle

Recommended Posts

Hello,

 

The following code does not return any errors, but nothing is ever inserted into the database. I'm sure it's something super simple :\

 

 

$result = mysql_query("SELECT filename FROM p_headerimages ORDER BY ordernum") or die(mysql_error());
$newcount = count($result) +1;
$query2 = "INSERT INTO `psitez_wrdp12`.`p_headerimages` (`filename`, `ordernum`) VALUES (\''$file_name'\', \''$newcount'\');";
$result2= mysql_query($query2);

 

 

 

Here is the database stuff

 

--
-- Table structure for table `p_headerimages`
--

CREATE TABLE IF NOT EXISTS `p_headerimages` (
  `filename` varchar(64) NOT NULL,
  `ordernum` tinyint(3) unsigned NOT NULL,
  PRIMARY KEY  (`ordernum`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1;

--
-- Dumping data for table `p_headerimages`
--

INSERT INTO `p_headerimages` (`filename`, `ordernum`) VALUES
('slideshow.jpg', 1),
('slideshow1.jpg', 2),
('slideshow2.jpg', 3),
('slideshow3.jpg', 4),
('slideshow4.jpg', 5),
('slideshow5.jpg', 6),
('slideshow6.jpg', 7);

 

 

 

 

any ideas?? Thank you for your help!

 

Link to comment
Share on other sites

try this

$result = mysql_query("SELECT filename FROM p_headerimages ORDER BY ordernum") or die(mysql_error());
$newcount = count($result) +1;
$query2 = "INSERT INTO `psitez_wrdp12`.`p_headerimages` (`filename`, `ordernum`) VALUES ('$file_name', '$newcount')";
$result2= mysql_query($query2) or die(mysql_error());

Link to comment
Share on other sites

When I set ordernum as 'auto-increment' and then do just

 

$query2 = "INSERT INTO p_headerimages (`filename`) VALUES ('$file_name')";

 

 

it works, but then the number that gets put into ordernum is not what I want it to be. Do I need to add another value like just id or something?

Link to comment
Share on other sites

$result = mysql_query("SELECT filename FROM p_headerimages ORDER BY ordernum") or die(mysql_error());
$newcount = mysql_num_rows($result)+1;
$query2 = "INSERT INTO p_headerimages (`filename`, `ordernum`) VALUES ('$file_name', '$newcount')";
$result2= mysql_query($query2) or die(mysql_error());

 

 

This is now working. Except the database does not seem to have a key or nothing is sorted. Do I need to worry about that?

Link to comment
Share on other sites

its not a huge deal, however it is a good practice to get into to use an auto-incrementing primary key field...for neatness. I normally set a field called "id" as my primary key, and make it auto-incrementing as well

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.